The SearchWP extension provides additional functionality for returning SearchWP query results to Ajax Load More for infinite scrolling.

This extension is available to users running Ajax Load More 2.13+ and SearchWP 2.6.1+.


How It Works

The extension works by using the alm_query_args filter to provide a connection point between Ajax Load More and SearchWP.

By utilizing the alm_query_args filter you can pass search parameters to the SWP_Query class for search results then send the returned data 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 SearchWP extension for search results.

<?php
/**
 * Use alm_query_args filter to pass data to SearchWP.
 * 
 * @see https://connekthq.com/plugins/ajax-load-more/docs/filter-hooks/#alm_query_args
*/
function my_alm_query_args_searchwp( $args ) {   
   $engine = 'default'; // default = default
   $args = apply_filters( 'alm_searchwp', $args, $engine );
   return $args;
}
add_filter( 'alm_query_args_searchwp', 'my_alm_query_args_searchwp' );
PHP

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 (searchwp).
    echo do_shortcode( '[ajax_load_more id="searchwp" 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