0

This is my function in my template's functions.php.

The problem is that this is working:

function ajax_search_action_do(){
    global $wp_query;

    $search = $_POST['search_value'];
    $country   = $_POST['country_value'];

    $args = array(
        's' => $search,
        'posts_per_page' => 10,
    );
    $wp_query = new WP_Query( $args );

    get_template_part( 'search-results' );

    exit;
}

But this is not:

function ajax_search_action_do(){
    global $wp_query;

    $search = $_POST['search_value'];
    $country   = $_POST['country_value'];



    $args = array(
        's' => $search,
        'posts_per_page' => 10,
        'tax_query' => array(                
          array(
            'taxonomy' => 'country',               
            'field' => 'id',                      
            'terms' => $country,   
          ),
        ),
    );
    $wp_query = new WP_Query( $args );

    get_template_part( 'search-results' );

    exit;
}

So, when there is search term alone, it's working and I get the right results. But when I try to combine it with the tax_query search term is not working and only tax_query works.

How to make both conditions work together?

THe query that is build:

`SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (8) ) AND (((wp_posts.post_title LIKE '%test%') OR (wp_posts.post_content LIKE '%test%'))) AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10`
  • inspect the query object and look at the SQL generated by your query. there's no obvious reason why that shouldn't work, in fact a copy/paste of your query works fine for me. – Milo Jul 16 '13 at 17:06
  • How to print the query? Via print_r($wp_query) ? Could you be more specific, please? – Derfder Jul 16 '13 at 17:28
  • $wp_query->request contains the actual query sent to the DB – Milo Jul 16 '13 at 17:32
  • I have updated my question with the query result. – Derfder Jul 16 '13 at 17:40
  • I see nothing wrong with that query. are you sure you have a post with title or content containing test and associated to that tax term? – Milo Jul 16 '13 at 17:44
2

This is a result of a problematic GROUP BY clause that's added to WP_Query when tax_query is present. It conflicts with the s parameter. You can remove the GROUP BY entirely to fix it, but you shouldn't do this for every WP_Query. I use the following class often to approach this:

/**
 * Search Within a Taxonomy
 * 
 * Support search with tax_query args
 *
 * $query = new WP_Query( array(
 *  'search_tax_query' => true,
 *  's' => $keywords,
 *  'tax_query' => array( array(
 *      'taxonomy' => 'country',               
 *      'field' => 'id',                      
 *      'terms' => $country,   
 *  ) ),
 * ) );
 */
class WP_Query_Taxonomy_Search {
    public function __construct() {
        add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
    }

    public function pre_get_posts( $q ) {
        if ( is_admin() ) return;

        $wp_query_search_tax_query = filter_var( 
            $q->get( 'search_tax_query' ), 
            FILTER_VALIDATE_BOOLEAN 
        );

        // WP_Query has 'tax_query', 's' and custom 'search_tax_query' argument passed
        if ( $wp_query_search_tax_query && $q->get( 'tax_query' ) && $q->get( 's' ) ) {
            add_filter( 'posts_groupby', array( $this, 'posts_groupby' ), 10, 1 );
        }
    }

    public function posts_groupby( $groupby ) {
        return '';
    }
}

new WP_Query_Taxonomy_Search();

Now you can add the custom search_tax_query to WP_Query objects that you want this functionality added to. For example:

$query = new WP_Query( array(
    'search_tax_query' => true,
    's' => $keywords,
    'tax_query' => array( array(
        'taxonomy' => 'country',               
        'field' => 'id',                      
        'terms' => $country,   
    ) ),
) );

To explain what's going on here, the problem is caused by an unintended result of the GROUP BY MySQL clause that's added to WP_Query objects with a tax_query argument. When you combine this with search, we don't want/need this clause. If you remove it, the search results will work as you expect: posts matching a search query (s) within a taxonomy (tax_query).

0

I am trying to create a wordpress custom search as well and chanced upon this post.

Try this?

function ajax_search_action_do(){
    global $wp_query;

    $search = $_POST['search_value'];
    $country   = $_POST['country_value'];

    $args = array(
        's' => $search,
        'posts_per_page' => 10,
        'tax_query' => array(
          'relation' => 'OR',                
          array(
            'taxonomy' => 'country',               
            'field' => 'id',                      
            'terms' => $country,   
          ),
        ),
    );
    $wp_query = new WP_Query( $args );

    get_template_part( 'search-results' );

    exit;
}
-1

I think that you're probably registering your custom taxonomies in the init action hook. However, if that's the case, init isn't called during an AJAX call.

To get it done, you could, by example, run manually the init action like that :

function ajax_search_action_do(){
   do_action('init'); 
   global $wp_query;
   ...
}
  • 1
    init is called on AJAX requests. – Milo Jul 16 '13 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.