Support » Plugin: Relevanssi - A Better Search » How to set filters to limit search results

  • Resolved tariqaltaf

    (@tariqaltaf)


    In my search results page I need to add filters(‘Total Number of records’) like Display 10 Records, Display 20 records, Display 30 Records so that when user click on that then the search results will be displayed accordingly to that filter limit. These filters will be in the form of links so no buttons or dropdowns.

    I am also using Wp Page Navi plugin to display search results with pagination and it is working fine.

    Kindly guide how can I add these filters

    thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Here’s how you can generate the links:

    $query_string = $_SERVER['QUERY_STRING'];
    $posts_10     = add_query_arg( 'posts_per_page', 10, $query_string );
    $posts_20     = add_query_arg( 'posts_per_page', 20, $query_string );
    $posts_30     = add_query_arg( 'posts_per_page', 30, $query_string );
    Thread Starter tariqaltaf

    (@tariqaltaf)

    thanks for this. But how to make a link?

    Plugin Author Mikko Saari

    (@msaari)

    <a href="<?php echo $posts_10;">Show 10 posts</a>
    <a href="<?php echo $posts_20;">Show 20 posts</a>
    <a href="<?php echo $posts_30;">Show 30 posts</a>
    • This reply was modified 2 years, 3 months ago by Mikko Saari.
    Thread Starter tariqaltaf

    (@tariqaltaf)

    Already tried that. It adds the query string in my url like below:
    http://localhost/project/?s=contact&posts_per_page=3

    But it cannot limit the search results. Here is my complete code:

    
    <?php
    
    /// Calling Custom Search Form ///
    		add_filter( 'get_search_form', 'custom_search_form_2' );
    		get_search_form();
    		remove_filter( 'get_search_form', 'custom_search_form_2' );
    if(have_posts()):		
    					/// Calling Custom Search Form ///
    		add_filter( 'get_search_form', 'custom_search_form_2' );
    		get_search_form();
    		remove_filter( 'get_search_form', 'custom_search_form_2' );
    		
    		/// Filteration ////
    $query_string = $_SERVER['QUERY_STRING']."<br>";
    
    $posts_10     = add_query_arg( 'posts_per_page', 3, $query_string );
    $posts_20     = add_query_arg( 'posts_per_page', 20, $query_string );
    $posts_30     = add_query_arg( 'posts_per_page', 30, $query_string );
    ?>
    
    <a href="<?php echo '?'.$posts_10;?>">Show 10 posts</a>
    
    <?php 
    while(have_posts()): 
    the_post();
    ?>
    
    

    Kindly guide what is the mistake because it cannot filter the number of results when I use the filtering link.

    Thanks

    Plugin Author Mikko Saari

    (@msaari)

    The problem is not in your code, but in WordPress. I’m pretty sure WP accepted the posts_per_page parameter before, but it doesn’t anymore. This is something I can fix in the next version of Relevanssi, but meanwhile you can add this to your theme functions.php:

    add_filter( 'query_vars', 'rlv_query_vars' );
    function rlv_query_vars( $qv ) {
    	$qv[] = 'posts_per_page';
    
    	return $qv;
    }

    Once you add this, the parameter will start working.

    Thread Starter tariqaltaf

    (@tariqaltaf)

    oh great thanks a lot for this help.

    Thread Starter tariqaltaf

    (@tariqaltaf)

    thanks again

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to set filters to limit search results’ is closed to new replies.