0

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 ) {}?>

<?php
$query = new WP_Query(array(
'post_type' => 'blogcom',
'tag_blogcom' => $term->slug,
'showposts' => 10
));
?>

1 Answer 1

2

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(
                                                array(
                                                'taxonomy' => 'tag_blogcom',
                                                'field' => 'slug',
                                                'terms' => $slugs,
                                                ),
                                            ),
                            'posts_per_page' => 10
                            )
                        );

Check the Taxonomy Parameters at WordPress Codex.

2
  • 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
    Commented Jan 7, 2014 at 8:34
  • @Newo1t, if it's working, please accept it as the answer to help people to find it easily.
    – Abhik
    Commented Jan 7, 2014 at 18:48

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.