Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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`
share|improve this question
    
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 Answers 2

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;
   ...
}
share|improve this answer
    
init is called on AJAX requests. –  Milo Jul 16 '13 at 18:24

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;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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