The Relevanssi extension provides additional functionality for returning Relevanssi search results to Ajax Load More.

This extension is available for users running Ajax Load More 2.13+ and Relevanssi.


How It Works

The extension provides a connection point between Ajax Load More and Relevanssi.

By utilizing the core alm_query_args filter, we can pass search parameters from a WordPress template to the relevanssi_do_query() function for results and then send those results back to Ajax Load More for display.

Implementation Steps

Step 1: Add Filter

Add the following Ajax Load More filter to your functions.php file – this filter will hook directly into the Relevanssi extension for search results.

<?php
/**
 * Use alm_query_args filter to pass data to relevanssi_do_query() then back to ALM.
 * 
 * @see https://connekthq.com/plugins/ajax-load-more/docs/filter-hooks/#alm_query_args
*/
function my_alm_query_args_relevanssi( $args ){
   $args = apply_filters( 'alm_relevanssi', $args );
   return $args;
}
add_filter( 'alm_query_args_relevanssi', 'my_alm_query_args_relevanssi' );
PHP

Note: Don’t forget to update the filter name to use your Ajax Load More ID.

Step 2: Template and Shortcode

In search.php or a custom template, create a shortcode with a unique ID and pass the search term to Ajax Load More using the search parameter.

<?php
/**
 * search.php 
 * WordPress search results template
*/
get_header();  
// http://website.com/?s=toronto+canada
$term = isset( $_GET['s'] ) ? $_GET['s'] : ''; // Get 's' querystring param.
?> 
<div id="container">
  <?php 
    // Ajax Load More shortcode with a unique ID parameter (relevanssi).
    echo do_shortcode( '[ajax_load_more id="relevanssi" search="'. esc_attr( $term ) .'" post_type="any"]' ); 
  ?>
</div> 
<?php get_footer(); ?>
PHP

Pro Tip: Set post_type="any" in your shortcode to return all post types.


FAQs