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 going crazy here as I assume it is quite simple, but I just cannot get it to work.

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'?

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

<?php
$args=array(
'post_type' => 'portfolio',
'locations' => 'paris'
);
$my_query = null;
$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();  
?>

Hope someone can help!

Thank you!

share|improve this question
2  
Are you sure locations is slug for that taxonomy? Could you show taxonomy registration code? –  Rarst Aug 1 '13 at 21:45
1  
That 'taxonomy' => 'term_slug' pattern is deprecated but should still work. Something else is wrong. Post debugging information please. –  s_ha_dum Aug 1 '13 at 22:08
    
Also the code tries to query a portfolio post_type 'post_type' => 'portfolio' but the question refers to "the posts of the taxonomy 'portfolio'". –  Jon Aug 2 '13 at 0:25
add comment

2 Answers

Add a slug to your taxonomy :

'rewrite' => array( 'slug' => 'taxonomy_slung' )
share|improve this answer
add comment

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!

share|improve this answer
add comment

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.