WordPress.org

Forums

Relevanssi - A Better Search
[resolved] relevanssi_do_query not honoring tax_query (10 posts)

  1. alex.ciarlillo
    Member
    Posted 2 years ago #

    the relevanssi_do_query function does not seem to honor 'tax_query' when creating a WP_Query object. Code:

    // setup query args
                $args = array('s' => $_POST['query'],
                    'post_type' => $post_type,
                    'post_status' => array('publish')
                );
    
                if(isset($post_tax)) {
                    $args['tax_query'] = array(
                                            array(
                                                'taxonomy' => $post_tax,
                                                'field' => 'slug',
                                                'terms' => $post_term
                                            )
                                            );
                }
    
                $query = new WP_Query($args);
    
    //            var_dump($query);
    
                relevanssi_do_query($query);

    If I comment out the last line my results are as expected but I lose other functionality of the relevanssi query engine.

    thanks

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

  2. Mikko Saari
    Member
    Plugin Author

    Posted 2 years ago #

    Correct. Instead of tax_query, use "taxonomy" and "taxonomy_term", and it should work. I'll add this to my to-do list.

  3. alex.ciarlillo
    Member
    Posted 2 years ago #

    Thanks for the quick reply. Just to clarify, I add these fields to the args array?

    I have tried this:

    if(isset($post_tax)) {
       $args['taxonomy'] = $post_tax;
       $args['taxonomy_term'] = "video"; // hard code for testing
    }

    and also using the term id:

    $args['taxonomy_term'] = 321;

    Still the same result (getting results that are not in the queried taxonomy).

    edit: Just want to mention I *am* indexing "faq-topic" as a custom taxonomy. I was not previously so I went back changed it and re-indexed, re-tried all methods listed above with same results.

  4. Mikko Saari
    Member
    Plugin Author

    Posted 2 years ago #

    I'm not sure - try putting them to $query->query_vars['taxonomy'] and $query->query_vars['taxonomy_term'] - that's where Relevanssi will be looking for them, since that's where they are when used in a query.

  5. alex.ciarlillo
    Member
    Posted 2 years ago #

    It has been some time since I was able to revisit this. I was hoping you might still have a suggestion or two to get this working.

    I tried your suggestion above with the following code:

    $query = new WP_Query($args);
    
                if(isset($post_tax)) {
                    // I have used debug output to determine I *am* in the loop
                    $query->query_vars['taxonomy'] = 'faq-topic';
                    $query->query_vars['taxonomy_term'] = 'video'; // id: 321 (tried both by slug and by id)
    
                }
    
                relevanssi_do_query($query);

    No luck with that either.

  6. Mikko Saari
    Member
    Plugin Author

    Posted 2 years ago #

    Well, that example is at least missing the search term ($query->query_vars['s']).

  7. alex.ciarlillo
    Member
    Posted 2 years ago #

    Here is the more complete code. The search parameter is set in the args array:

    // setup query args
                $args = array('s' => $_POST['query'],
                    'post_type' => $post_type,
                    'post_status' => array('publish')
                );
    
                $query = new WP_Query($args);
    
                if(isset($post_tax)) {
                /*
                    $args['tax_query'] = array(
                                            array(
                                                'taxonomy' => 'faq-topic',
                                                'field' => 'id',
                                                'terms' => array(321)
                                            )
                                        );
                 */ 
    
                    $query->query_vars['taxonomy'] = 'faq-topic';
                    $query->query_vars['taxonomy_term'] = 'video'; // id: 321
                }
    
                relevanssi_do_query($query);

    edit: in case you are wondering why this might appear unorthodox for a search results page it is because it is actually being used to return the results to an AJAX call in order to populate a drop down with search hints similar to Wikipedia.

  8. Mikko Saari
    Member
    Plugin Author

    Posted 2 years ago #

    I'm currently rewriting the whole taxonomy search code to work with tax_query, but that's how it should work in the current version.

  9. Mikko Saari
    Member
    Plugin Author

    Posted 2 years ago #

    I just released 3.1, which now supports tax_query.

  10. alex.ciarlillo
    Member
    Posted 2 years ago #

    Great to hear. Cannot wait to give it a try.

    thanks

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Relevanssi - A Better Search
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic