I'm currently using the following bit of code to display related posts on my single template.

        $orig_post = $post;
        global $post;
        $tags = wp_get_post_tags($post->ID);

        if ($tags) {
            $tag_ids = array();
            foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
                'tag__in' => $tag_ids,
                'post__not_in' => array($post->ID),
                'posts_per_page'=>5, // Number of related videos that will be shown.

            $my_query = new wp_query( $args );
            if( $my_query->have_posts() ) {
                echo '<section id="related-videos"><h3><span>Related Videos</span></h3><ul>';

                while( $my_query->have_posts() ) {
                $my_query->the_post(); ?>

                    <a class="related-thumb" href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('related-thumb'); ?></a>
                    <section class="related-content">
                        <a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>

                <? }

                echo '</ul></section>';

        $post = $orig_post;

I'm wondering if there is a way I can pull tags from only a certain post type. Maybe adding something like 'post_type=videos' somewhere? Not sure where, though. Can someone help me out? Thank you.

  • @Maugly Thank you, that worked. Just wished you posted this as an answer so I can accept it.
    – J82
    Feb 20, 2011 at 18:50
  • Some wishes come true so easily ;)
    – Michal Mau
    Feb 20, 2011 at 19:40

3 Answers 3


Something exactly like 'post_type' => 'videos' somewhere in your $args array :)


I am not sure from your description - do you want to only display this for posts of specific type or only display posts of specific type as related ones?

Your guess is precise, query does take post_type argument, see Type & Status Parameters in Codex.

  • I'd like to display posts of a specific post type as the related posts. I just am not sure where that post_type argument should go.
    – J82
    Feb 20, 2011 at 18:40
  • @John it should go with rest of arguments you set up for query in $args=array(
    – Rarst
    Feb 20, 2011 at 19:55

I couldn't find a way to filter via post type when retrieving a list of tags. I would do something like this:

function get_tags_per_post_type() {
    $post_list = get_posts(array('post_type' => ''));
    $tag_list = array();
    foreach($post_list as $current_post) {
        $current_tag_list = wp_get_post_tags($current_post->ID);
        foreach ($current_tag_list as $tag) {
            $tag_list[] = $tag->term_id;

    $tag_list = array_unique($tag_list);
    return $tag_list;

This retrieves a list of tag term ids related to a particular post type. If you want a list of tag objects, then we would need to create a function which identify duplicate tag object being added to $tag_list.


  • Thank you for helping, rxn. However Maugly offered a simple solution to add the argument and it works fine.
    – J82
    Feb 20, 2011 at 18:51

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.