Support » Plugin: Relevanssi - A Better Search » Limit Ajax search to 5 posts

  • Resolved Thomas Jarvis

    (@thomasjarvisdesign)


    The site in the link is based on Enfold theme.

    When Relevanssi is installed the Ajax search results limit seems to match the throttle limit when throttle results is turned on. So we get up to 500 results in the ajax result.

    Is there a way in Relevanssi to set a limit on how many ajax results are shown and not affect the throttle total on the full results page?

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter Thomas Jarvis

    (@thomasjarvisdesign)

    If I do this from the documentation the ajax search drops to 5 but the full search page results also drop to 5.

    Is there a way to get this to only apply to the ajax dropdown?

    add_filter( 'pre_option_relevanssi_throttle_limit', function( $limit ) { return 5; } );

    Plugin Author Mikko Saari

    (@msaari)

    Never set the throttle to a value less than 100-200, that just gets you random results. The setting you’re looking for is posts_per_page, and for Live Search specifically you can use the hook relevanssi_live_search_posts_per_page.

    add_filter( 'relevanssi_live_search_posts_per_page', function() { return 5; } );

    Edit: But ah, you’re not using Relevanssi Live Ajax Search, but some theme-specific live search… For that, you can check the theme support to see if there’s a better method, but you can use the posts_per_page to control this. I’m sure there’s a way to restrict only to the live search (maybe check the query variables for something indicative), but I don’t know what that is as I don’t know the Enfold theme.

    Thread Starter Thomas Jarvis

    (@thomasjarvisdesign)

    Thanks Mikko,

    Can you confirm I have this in the right format? See my functions.php code below:
    posts_per_page and relevanssi_live_search_posts_per_page is being ignored. Only pre_option_relevanssi_throttle_limit is impacting the Ajax search and search page.

    add_filter( 'posts_per_page', function() { return 5; } );
    add_filter( 'relevanssi_live_search_posts_per_page', function() { return 5; } );
    add_filter( 'pre_option_relevanssi_throttle_limit', function( $limit ) { return 200; } );


    I have raised a ticket with Enfold support and will also share a link to this post if you can get back to me about the forat of the above.

    Plugin Author Mikko Saari

    (@msaari)

    posts_per_page is a query variable, not a filter hook. It works like this:

    add_filter( 'relevanssi_modify_wp_query', function( $query ) {
      $query->set( 'posts_per_page', 5 );
      return $query;
    } );

    The Relevanssi Live Ajax Search filter hook doesn’t work, because you’re using Enfold live search, not Relevanssi Live Search.

    Thread Starter Thomas Jarvis

    (@thomasjarvisdesign)

    Thanks Mikko, I have raised a ticket with Enfold to see if they can update their settings. If not I’ll consider trying the relevanssi live search (I assume you mean the extra plugin).

    I have commented out your code correction for now. The only way to adjust the ajax by default with enfold is to use pre_option_relevanssi_throttle_limit as far as I can tell.

    Open ticket on Enfold is here:

    https://kriesi.at/support/topic/enfold-relevanssi-ajax-dropdown-limit/

    I’ll see if they have any suggestions. For now I will limit the result numbers to between 50-200 so at least the Ajax search has some limits.

    Thread Starter Thomas Jarvis

    (@thomasjarvisdesign)

    Hi Mikko,

    Gunter at Enfold has provided the following code which works as expected:

    if( wp_doing_ajax() )
    {
        add_filter( 'pre_option_relevanssi_throttle_limit', function( $limit ) { return 5; } );
    }

    I may adjust this to 25. I see what you mean about the accuracy of the search results.

    Plugin Author Mikko Saari

    (@msaari)

    That’s still not good. The throttle option is not designed for this purpose. It’s designed to be set at 500 posts to allow decent performance on big sites. It is not for controlling the number of results. It will make the results much worse.

    Please ask the Enfold support if there’s a way to adjust the posts_per_page parameter for the search. This parameter is made for this purpose, and will ensure the maximum quality of results.

    Thread Starter Thomas Jarvis

    (@thomasjarvisdesign)

    Hi Mikko,

    Gunter has provided another solution for this. It seems to work but is giving me different results to the search page. But the more specific the search the better the outcome is.

    enfold\config-relevanssi\class-avia-relevanssi.php

    Around line 170 you find:

    
    			$tempquery->query_vars = $search_parameters;
    			relevanssi_do_query( $tempquery );
    

    Try the following:

    
    			$tempquery->query_vars = $search_parameters;
    			$tempquery->set( 'posts_per_page', 5 );
    			relevanssi_do_query( $tempquery );
    

    Thread Starter Thomas Jarvis

    (@thomasjarvisdesign)

    Enfold are adding a filter to version 5.6.6 to address the problem. I am satisfied that it works as intended.

    Thank you for your help.

    Plugin Author Mikko Saari

    (@msaari)

    Yes, that’s the correct solution. It’s good that they’re adding a filter there.

    Thread Starter Thomas Jarvis

    (@thomasjarvisdesign)

    Yeah the guys at Enfold offer some of the best theme support I’ve ever encountered.

    They have frequently added my suggestions to the core theme over the past 5 years.

    There is also a small CSS fix as well that puts the ajax results into a dropdown:

    .ajax_search_response {
        max-height: 300px;
        overflow-y: scroll;
    }
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Limit Ajax search to 5 posts’ is closed to new replies.