Get posts of ONE taxonomy term of custom post type

I have a custom post type called portfolio and a taxonomy called locations. In the taxonomy I have various terms, like paris, berlin, london etc.

How can I get only the posts which are related to one term, e.g. paris?

Read More

I am trying the following code, yet it gives me the ALL the posts of the taxonomy portfolio and not just of one term:

$args = array(
    'post_type' => 'portfolio',
    'locations' => 'paris'
);
$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
    endwhile;
}
wp_reset_query();

Related posts

2 comments

  1. You can try making the $args array more specific with:

    $args = array(
        'post_type' => 'portfolio',
        'tax_query' => array(
            array(
                'taxonomy' => 'locations',
                'field' => 'slug',
                'terms' => 'paris'
            )
        )
    );
    

    Grabbed this snippet similar to one on the WP Query page in the codex. Also, this is assuming that your custom post type is called “portfolio” and you have a custom taxonomy, “locations” and the term “paris”. I say this because your question above says:

    I am trying the following code, yet it gives me the ALL the posts of
    the taxonomy ‘portfolio’ and not just of one term:

    So “portfolio” is your custom post type, correct? Anyway, let us know if you find the problem. Good luck!

  2. Add a slug to your taxonomy :

    'rewrite' => array( 'slug' => 'taxonomy_slung' )
    

Comments are closed.