0

I need to do a wp query that will get any post that has one tag or more. My current code works for one tag, but I've try for multiple with the codex and I can't do it. Can you help me?

<?php
global $post;
foreach(get_the_tags($post->ID) as $tag) {
}

$recent_posts = new WP_Query(array(
    'cat'  => 20,
    'tag_slug__in' => $tag->name . ', ',
    'showposts' => 2
));
?>
3

If you read the Codex entry, you must pass an array as tag_slug__in. In this case though, I would use tag__in with IDs rather than slugs:

$tag_ids = array();
foreach( get_the_tags($post->ID) as $tag ) {
    $tag_ids[] = $tag->term_id;
}
$recent_posts = new WP_Query(
    array(
        'tag__in' => $tag_ids,
        'posts_per_page' => 2,
        'cat' => 20
    )
);

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.