WordPress.org

Support

Support » Plugins and Hacks » [Resolved] [Plugin: Relevanssi – A Better Search] display results differing

[Resolved] [Plugin: Relevanssi – A Better Search] display results differing

  • Hi
    I am using Woocommerce and Relevanssi, so that we can search by SKU in front and back end. This works great.

    However, our search results pages have different numbers of products on them. For instance there will be 19 pages of results in a search for “red”, the first page will have our specified 16 products but later pages show 2 or 6 or 0 etc.

    I am wondering if it is to do with the fact that we have products that are not active. In WOOCommerce this is a meta key of _visibility.
    I am assuming that these are the ones missing from the search pages.
    I realise you can add meta key’s to the search index but is it possible to remove them from the search results before the pagination gets computed.
    thanks
    elvis

    http://wordpress.org/extend/plugins/relevanssi/

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

    @msaari

    Yes, there are ways to do it.

    I’d do this with a filter function on relevanssi_hits_filter that would remove all non-visible posts from the search results. (See Relevanssi filter hooks)

    I’ll have to look at how to do that. Will that make the pagination correct?
    Would it not be better to do it before the results are collected?

    I did have a look at “relevanssi_modify_wp_query”

    and the query already contains the meta_query for selecting only those selected as “visible” or “search”. (in woo you can have them as hidden/visible/search only/catalogue only)
    So does this mean that Relevanssi is discarding that part of the query?

    Alternatively, would it be possible to remove the “hidden” ones from the index?

    Plugin Author Mikko Saari

    @msaari

    Yes, Relevanssi doesn’t use that meta query, because Relevanssi is not querying the posts database.

    You can use ┬┤relevanssi_do_not_index` filter to keep the hidden posts from index.

    add_filter('relevanssi_do_not_index', 'no_hidden');
    function no_hidden($index, $id) {
        if (get_post_meta($id, '_visibility', true) == 'hidden') return false;
        return true;
    }

    Something like this.

    Hi,
    I tried this code and got an error that the function “no_hidden” was not receiving the second parameter.
    The only way I can get this to work correctly is to change the Relevanssi code from

    if (true == apply_filters('relevanssi_do_not_index', false, $post->ID)) {
    to

    if (true == apply_filters('relevanssi_do_not_index', $post->ID)) {

    (ie. remove the ‘false’)

    and to use this as my function (true/false swapped)

    function no_hidden($id) {
        if (get_post_meta($id, '_visibility', true) == 'hidden'){
         return true;
        } else { return false;}
    }

    It looks like the “false” was confusing it, unless you have a solution that doesn’t need me to edit the plugin!
    cheers
    elvis

    Hi
    have now realised this doesn’t work as I still need to see the “hidden” ones in the back end, and not indexing them means they don’t show in searches there.

    I have tried again to use
    relevanssi_hits_filter

    I have successfully removed the posts from the $hits array using it but the page navigation numbers still show the numbers from the full result not the hits once filtered.
    ie. i have reduced the number of posts in array($hits) to 15, but I get a search page which says there are 7 pages of results, the first page showing no posts.

    Is there a better filter to use to remove the posts from the post count?

    cheers
    elvis

    Plugin Author Mikko Saari

    @msaari

    Relevanssi_hits_filter is applied before the posts array is built, so the count should be correct. Where are you getting the number of posts found? $wp_query->post_count should be correct, it’s set as the last thing Relevanssi does, after all filters.

    Plugin Author Mikko Saari

    @msaari

    Is your page navigation compatible with Relevanssi? Some page navigations assume WP search and page incorrectly with Relevanssi.

    Hi Mikko
    yes you are right, the post_count is correct.

    I’m using WP-PageNavi, but I get the same if i turn that off and use the wordpress default navigation (it doesn’t show me the number of pages but it has the same number of “next” and the first page doesn’t show any results on my test search.)

    Can you tell me which page navigation is compatible with Relevanssi, or are there settings I can use for WP-PageNavi?
    thanks
    elvis

    Hi Mikko

    having looked at it again. I realise that I hadn’t got the relevanssi_hits_filter to successfully work.

    If I print out in my relevanssi_hits_filter function my resulting array, it contains the correct array of Post objects. But if I print out the post list on the results page, it doesn’t contain the same list of posts.

    this is my filter code.

    add_filter('relevanssi_hits_filter', 'remove_hidden');
    
    function remove_hidden($params){
    	if(!is_admin()){
    		foreach($params[0] as $pos =>$result){
    			if (get_post_meta($result->ID, '_visibility', true) == 'hidden'){
    			    unset($params[0][$pos]);
    		    }
    		}
    		return(array($params[0]));
    	} else {
    		return(array($params[0]));
    	}
    }

    With my test search, printing out $params[0] shows an array with 15 posts objects(the correct ones). My search results page shows 1 post, the first one from the $params list.
    Printing out wp-query, i can see that
    found_posts = 15
    but posts = an array with 1 post object.

    Do you have any ideas on this? I’m not sure where i’m actually going wrong!

    thanks
    elvis

    Plugin Author Mikko Saari

    @msaari

    Show me the code for your search results page. There’s probably something wrong there. Your filter function seems to be working correctly.

    I am using the templates that comes with Woocommerce.

    Mikko fixed it!
    the filter should go like this….

    add_filter(‘relevanssi_hits_filter’, ‘remove_hidden’);

    function remove_hidden($params){
    if(!is_admin()){
    $visible_posts = array();
    foreach($params[0] as $pos =>$result){
    if (get_post_meta($result->ID, ‘_visibility’, true) == ‘visible’){
    $visible_posts[] = $result;
    }
    }

    return(array($visible_posts));
    } else {
    return(array($params[0]));
    }
    }
    For information. This works for me as my products in woocommerce are hidden or visible, you would need to adapt if using ‘search’ or ‘catalog’ only options.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘[Resolved] [Plugin: Relevanssi – A Better Search] display results differing’ is closed to new replies.