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 works by providing a connection point between Ajax Load More and Relevanssi.
By utilizing the core alm_query_args filter, we are able 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.
1 2 3 4 5 6 7 8 9 10 11 |
<?php /* functions.php Use alm_query_args filter to pass data to relevanssi_do_query() then back to ALM. 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'); |
Step 2: Template and Shortcode
In search.php or a custom template, create a shortcode with unique ID and pass the search term to Ajax Load More using the search parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php /* search.php Our 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 (searchwp). echo do_shortcode('[ajax_load_more id="relevanssi" search="'. $term .'" post_type="post, page, portfolio"]'); ?> </div> <?php get_footer(); ?> |
Pro Tip: Don’t forget to set your post types. Relevanssi will NOT accept post_type=”any”.
FAQs
To highlight the current search term you need to replace the_excerpt() function in your Ajax Load More Repeater Template with relevanssi_do_excerpt() and pass in the term and $post object as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<div class="template"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php $global $post; // Get global $post object $url = $_SERVER["HTTP_REFERER"]; // Get referring URL $parts = parse_url($url); // Parse the URL parse_str($parts['query'], $querystring); // Get the querystring $term = (isset($querystring['s'])) ? $querystring['s'] : ''; // Your querystring search parameter echo relevanssi_do_excerpt($post, $term); // the_excerpt(); ?> </div> |