I need to do a wp query that will get any post in custom post type that has one custom taxonomy (not hierarchical, like a post tag) or more. My current code work for one tag, but I've try for multiple and I can't do... I've try a lot of possibilites, query by taxonomy ID etc, no one works for me. Can you help me ? Thank's !

There is my actual code, work for one tag :

<?php foreach (get_the_terms( $post->ID, 'tag_blogcom' ) as $term ) {}?>

$query = new WP_Query(array(
'post_type' => 'blogcom',
'tag_blogcom' => $term->slug,
'showposts' => 10

Try this..

$terms = get_the_terms( $post->ID, 'tag_blogcom' );
    foreach ($terms as $term) {
        $slugs[] = $term->slug;

    $query = new WP_Query( array(
                            'post_type' => 'blogcom',
                            'tax_query' => array(
                                                'taxonomy' => 'tag_blogcom',
                                                'field' => 'slug',
                                                'terms' => $slugs,
                            'posts_per_page' => 10

Check the Taxonomy Parameters at WordPress Codex.

  • Obviously... I'ts working, but i've try earlier by putting everything in the same <?php ?>. In this case, this send me a warning : invalid argument for foreach, I need to make the get the terms and foreach in another <?php ?> in the top of my file. Thank's – Newo1t Jan 7 '14 at 8:34
  • @Newo1t, if it's working, please accept it as the answer to help people to find it easily. – Abhik Jan 7 '14 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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