The SearchWP extension provides additional functionality for returning SearchWP query results to Ajax Load More for infinite scrolling.
This extension is available 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 are able pass a 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.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php /* functions.php Use alm_query_args filter to pass data to SearchWP. 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); // Make call to alm_searchwp filter return $args; } add_filter( 'alm_query_args_searchwp', 'my_alm_query_args_searchwp'); |
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 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="'. $term .'" post_type="any"]'); ?> </div> <?php get_footer(); ?> |
Pro Tip: Set post_type=”any” in your shortcode to return all post types.
FAQs
Highlighting the current search term in your SearchWP results can be done using the following example in your Repeater Template.
1 2 3 4 5 6 7 8 |
<div class="template"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php global $post; $excerpt = get_the_excerpt( $post ); echo alm_searchwp_highlight( $excerpt, $args ); ?> </div> |
The code sample above hooks directly into the core SearchWP Highlighter function to highlight the search term within the results.