Listings and Search Templating

Overview

Templates are rendered using handlebars both on server (with PHP) and client side (with javascript).

  1. First page render is done with Handlebars PHP server side.
  2. When user triggers a search by using filters or text input, the results are loaded through AJAX and rendered client side with handlebars JAVASCRIPT.

Keep in mind that there are two different render for the same results, one for PHP and one for javascript.

Example: Using findstr to Create a Search Group

This example demonstrates how to initialize a findstr group, set custom query arguments, apply filters, retrieve results, and display them using a template.

Step-by-Step Guide

  1. Initialize the Group

Initialize a findstr group with a unique name. This name must be unique to avoid conflicts if multiple groups are used on the same page.

$findstr = findstr('my_first_group');
  1. Set Custom Query Arguments

Set custom query arguments to control the search behavior. In this example, we set the number of results per page to 12.

$findstr->set_query_arg('hitsPerPage', 12);
  1. Set Custom Sorting

Define custom sorting for the search results. Here, results are sorted by post_date in descending order and post_title in ascending order.

$findstr->set_query_arg(
  'sort',
  array(
    'post_date' => 'desc',
    'post_title' => 'asc',
  )
);
  1. Apply Default Search Filters

Apply default search filters if needed. In this example, we filter the results to only include posts of type post.

$findstr->add_filter( 'post_type', 'post' );
  1. Retrieve and Display Results

Retrieve the search results after setting all filters and query arguments. The results are then displayed using a template located in the plugin folder (templates/results.hbs.php).

Note:
The display_results() method should be called after setting all filters and query arguments, including all display_field() calls.

$findstr->display_results( true ); // Set to true to display the results, false to set empty results
  1. Display Pagination Field

Display the pagination field.

Note that the field must be created in the admin panel.

$findstr->display_field('pagination');

Next steps: Templating with handlebars

Meilisearch Documentation

Available query args in meilisearch documentation : meilisearch.com/docs/reference/api/search