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.

I am writing a query to retrieve instances of a custom post type I created called "Specialist". In the query I specify a name and more than one taxonomy.
I want to retrieve just those specialist which respect both parameters.
If I run the two queries separately, the correct specialists are returned but when I integrate the two in one single query, it seems that the post_name takes the priority on the taxonomy.

For example, if I run the query asking for a specialist named test with taxonomy therapist, I have back the specialist called test even if it does not have a therapist taxonomy.

Here is the code:

$args = array(
    'post_type'=>'specialist',
    'name' => 'test',
    'tax_query' => array(
        array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => 'therapist'
        )
    )
);

$specialists = new WP_Query($args);
share|improve this question
    
Please care about code formatting. –  kaiser Oct 29 '12 at 10:44
    
Why would you specify both a post name (i.e. implying the return of a single post) and a tax query (i.e. implying the return of potentially several posts)? Shouldn't it be one or the other? –  Chip Bennett Oct 31 '12 at 1:55

2 Answers 2

Yes your query is wrong, your are querying post name 'Test' which is 'specialist' post type and taxonomy is 'therapist'.

correct one should be any post_name from 'specialist' post type with which has Type taxonomy and term is 'therapist'.

$args = array(
'post_type'=>'specialist',
'tax_query' => array(
    array(
        'taxonomy' => 'type',
        'field' => 'slug',
        'terms' => 'therapist'
    )
)
);

specialists = new WP_Query($args);

I think this should be fine.

share|improve this answer

Is your post type non-hierarchical like a post - or hierarchical like a page? It will determine which slug parameter you should be using in WP_Query:

name (string) - non-hierarchical (post) slug.
pagename (string) - hierarchical (page) slug.

Also, the tax_query should have the operator parameter. Possible values are 'IN', 'NOT IN', 'AND'. You should set it to 'AND'. This could be a solution:

$args = array(
    'post_type'=>'specialist',
    'postname' => 'test',
    'tax_query' => array(
        array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => 'therapist',
            'operator'=> 'AND'
        )
    )
);

$specialists = new WP_Query($args);

If it really isn't working, then you could always do a workaround by creating a filtering function to add the where clause to the query

function filter_where( $where = '' ) {
    // post_name
    $where .= " AND $wpdb->posts.post_name = 'test'";
    return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $args );
remove_filter( 'posts_where', 'filter_where' );
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.