0

I have a custom post type "restaurants" and a taxonomy "location", so I can get the restaurants for one location by URL. On the taxonomy page for one specific location (CityA) I want to display all post types from restaurants and the taxonomy term "CityA". However, Wordpress also gives me posts with taxonomy terms "CityB" or "CityC" in the very same query. Have a look:

$args = array ( 'post_type' => 'restaurants', 'tax_query' => array ( 'taxonomy' => 'location', 'field' => 'slug', 'terms' => 'citya' ), 'order_by' => 'title', 'order' => 'ASC' );

$query = new WP_Query ( $args );
while ( $query -> have_posts () ) {
    $query -> the_post();
    echo get_permalink($post->ID;
}

This gives me ALL posts of post_type "restaurants". And yes, the slug for CityA is definetely "citya", I double checked all arguments.

1
  • You should indent your code properly, as it stands it is hard to read Commented Jul 24, 2014 at 11:42

1 Answer 1

0

As written here, you need to use two nested arrays in the tax_query argument.

So, with your arguments it should look like this:

$args = array ( 'post_type' => 'restaurants', 'tax_query' => array (array( 'taxonomy' => 'location', 'field' => 'slug', 'terms' => 'citya') ), 'order_by' => 'title', 'order' => 'ASC' );

If you only need to query for 1 Term, you can also use the easy way:

$args = array ( 'post_type' => 'restaurants', 'location' => 'citya' , 'order_by' => 'title', 'order' => 'ASC' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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