Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

    <?php
        $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;
            $args=array(
                'tag__in' => $tag_ids,
                'post__not_in' => array($post->ID),
                'posts_per_page'=>5, // Number of related videos that will be shown.
                'caller_get_posts'=>1
            );

            $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(); ?>

                <li>
                    <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>
                    </section>
                </li>

                <? }

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

        $post = $orig_post;
        wp_reset_query();

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.

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

3 Answers 3

up vote 1 down vote accepted

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

share|improve this answer

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.

share|improve this answer
    
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 '11 at 18:40
    
@John it should go with rest of arguments you set up for query in $args=array( –  Rarst Feb 20 '11 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.

HTH

share|improve this answer
    
Thank you for helping, rxn. However Maugly offered a simple solution to add the argument and it works fine. –  J82 Feb 20 '11 at 18:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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