danielmeade
danielmeade

Reputation: 174

Wordpress loop - Display posts by tag from differing post types

I'm trying to display posts within the WP loop and am able to successfully do so with <?php query_posts('tag_id=10'); ?>Here the loop will display all posts with the tag ID of 10, but I'd also like the loop to display posts from within a Custom Post type by the same tag.

I'm able to successfully display posts with tag_id=10 that originate from a custom post type using <?php query_posts('tag_id=10&post_type=videos'); ?>

But how can I merge the two?

I gave this a shot: <?php query_posts('tag_id=10, tag_id=10&post_type=videos'); ?> but that had no effect.

Any ideas on this one?

Upvotes: 2

Views: 4143

Answers (2)

Foxinni
Foxinni

Reputation: 4000

This runs an action before the posts are actually queried, thus altering the original output to your specific needs.

function tag_archive_mod( $query ) {

   if ( is_tag() && $query->is_main_query() ){

        $query->set('post_type',array('post','video'));

   }
}
add_action('pre_get_posts', 'tag_archive_mod');

Very, very useful. http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Upvotes: 1

The Alpha
The Alpha

Reputation: 146191

You can use this

query_posts( 
    array(
        'post_type' => array('post', 'videos'),
        'tag_id' => 10
));
while (have_posts()) : the_post();
    // loop code
endwhile;
wp_reset_query();

Upvotes: 2

Related Questions