• Hi,
    I’m building a Relevanssi integration extension for my plugin Ajax Load More but I cannot get relevanssi_do_query($query) to work in my the Ajax filter.

    From Ajax Load More I dispatch a call to a custom alm_relevanssi_get_posts() filter to return some post IDs to Ajax Load More, however relevanssi_do_query($query) always returns empty when used in the Ajax call.

    Here is the filter.

    function alm_relevanssi_get_posts($args){
    
       if(function_exists('relevanssi_do_query')){         	
       
          $query = new WP_Query($args);
          relevanssi_do_query($query);           	
          
          if ( $query->have_posts() ) {
             $the_posts = '';
             
             while ( $query->have_posts() ) {
                $query->the_post();
                echo $post->ID; 
                $the_posts[] = $post->ID;      				
             }
             	
             $args['post__in'] = $the_posts;
             $args['orderby'] = 'post__in'; // override orderby to relevance
             $args['s'] = ''; // Reset 's' term value      			
          }  
       
          return $args;	
       }  
    
    }

    FYI – I can confirm relevanssi_do_query($query) works correctly in my standard search template, it’s just failing when being called with Ajax.

    Hope this make sense to you…. Any ideas?

    Cheers,

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

    (@msaari)

    Relevanssi does some checks for AJAX situations, but those happen before Relevanssi goes into relevanssi_do_query(), that should just produce results.

    Can you show me what’s inside the $args when you run the code?

    By the way, if you only want post IDs out of Relevanssi, set the “fields” parameter to “ids”, and that’s what you will get.

    Thread Starter Darren Cooney

    (@dcooney)

    @msaari thanks for the quick reply!

    Inside of $args is the following array:

    Array
    (
        [post_type] => Array
            (
                [0] => any
            )
    
        [posts_per_page] => 5
        [offset] => 0
        [order] => DESC
        [orderby] => date
        [post_status] => publish
        [ignore_sticky_posts] => 
        [paged] => 1
        [s] => test
    )

    These $args are set inside Ajax Load More and passed to the Relevanssi filter.

    As I said, Relevanssi always returns empty from this query and never makes it past the initial if(have_posts()) statement.

    Plugin Author Mikko Saari

    (@msaari)

    Nothing problematic there. Solving this takes closer debugging. I’d look at the MySQL query Relevanssi generates (you can see it with the relevanssi_query_filter filter hook).

    Another thing you can look at is what relevanssi_hits_filter returns – does it return any posts? Perhaps take a look at what the search parameters are in the relevanssi_search_filters hook?

    Thread Starter Darren Cooney

    (@dcooney)

    Hi Mikko.
    I figured it out.

    For whatever reason relevanssi_do_query does not like $arg['post_type'] = 'any';

    It will always return zero results.
    When I switched it to $arg['post_type'] = 'page'; it started to work as expected.

    Is that a bug? Or maybe it’s just not supported.

    Cheers,

    Plugin Author Mikko Saari

    (@msaari)

    Setting it to “any” should work, though in that case it does lead to the post type parameter being left empty, with no post type restrictions in place (because you just can’t go comparing to MySQL and looking for the post type “any”). But that should work.

    So a bug, perhaps, or just a strange coincidence.

    Thread Starter Darren Cooney

    (@dcooney)

    Understood, thanks for your time!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘relevanssi_do_query in Ajax hook’ is closed to new replies.