PHP Usage
Main function to use in your theme is findstr()
. It returns a new instance of the Findstr
class.
<?php
// Init group with a unique name.
$findstr = findstr( 'my_uniq_group' );
Once you have an instance of Findstr
, you can use the following methods:
Set query args
Set custom query args, see Meilisearch doc for all available args
$findstr->set_query_arg( 'arg', 'value' );
Add filter
If needed, set custom default search filters, in this example we only search for posts type 'post'
$findstr->add_filter( 'post_type', 'post' );
Display field
Display the field. (the field must be created in admin panel). First parameter is the field name, second is an array of attributes that will be added to the field.
$findstr->display_field( 'search', array( 'placeholder' => "Let's find content..." ) );
Get results
Grab the results.
Note, $findstr->get_results()
should be called after setting all the filters and query args,
including display_field()
calls.
$results = $findstr->get_results();
Display results
Display the results. The template is located in the plugin folder in templates/results.hbs.php
Note that you can override it in your theme folder in findstr/results.hbs.php
, see Frontend-templating.md for more information.
// $results can be an array of results or true to display the results without calling get_results() before
$findstr->display_results( $results );