1

I created a custom post type named portfolio, and am trying to create a query to get similar posts from the portfolio post type based on if it has at least 1 similar tag.

For some reason this code displays tags from ANY post type, and if I remove the tags code, only posts from the portfolio type are displayed.

<div id="relatedbizposts">
    <h3 class="widgettitle">Related Businesses</h3>
    <ul id="relatedlist">
        <?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(
                'post_type'        => 'portfolio',
                'tag__in'          => $tag_ids,
                'post__not_in'     => array( $post->ID ),
                'posts_per_page'   => 8, // Number of related posts that will be shown.
                'caller_get_posts' => 1
            );
            $my_query = new wp_query( $args );
            if ( $my_query->have_posts() ) {
                while ( $my_query->have_posts() ) {
                    $my_query->the_post(); ?>
                    <li>
                        <div class="relatedthumb">
                            <a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
                                <?php the_post_thumbnail('small'); ?>
                            </a>
                        </div>
                    </li>
                <? }
            }
        }
        $post = $orig_post;
        wp_reset_query(); ?>
    </ul>
</div>
3
1

Have you tried wp_get_post_terms()

Example usage: $term_lists = wp_get_post_terms($post_id, 'product_tag', array("fields" => "names"));

I modified your code and got this to work on my site:

<ul id="relatedlist">
        <?php 

        $tags = wp_get_post_terms($post_id, 'product_tag');
        if ( $tags ) {
            $tag_ids = array();
            foreach ( $tags as $individual_tag ) array_push($tag_ids,$individual_tag->term_id);

            $args = array(
                'post_type'        => 'product',
                'tax_query' => array( 
                                array( 'taxonomy' => 'product_tag', 
                                        'field' => 'id',
                                        'terms' => $tag_ids
                                      )
                                    ),
                'post__not_in'     => array( $post_id ),
                'posts_per_page'   => 8, // Number of related posts that will be shown.

            );
            $my_query = new WP_Query( $args );

            if ( $my_query->have_posts() ) {
                while ( $my_query->have_posts() ) {
                    $my_query->the_post(); ?>
                    <li>
                        <div class="relatedthumb">
                        x
                            <a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
                                <?php the_post_thumbnail('small'); ?>
                            </a>
                        </div>
                    </li>
                <? }
            }
        }

        wp_reset_query(); ?>
    </ul>

note: caller_get_posts is deprecated since version 3.1

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.