0
votes

I created two different custom post types: "Videos" and "Locations."
I then created a custom taxonomy called "Video_Categories."
I have assigned this custom taxonomy to both of the custom post types.

What I want to do is display the Videos that have the same terms as one another on the Locations.

For example:

Video posts:

  • Name: Video 1; Video_Category: Brisbane, Queensland;
  • Name: Video 2; Video_Category: Gold Coast, Queensland;
  • Name: Video 3; Video_Category: Sunshine Coast, Queensland;

Location post:

  • Name: Brisbane; Video_Category: Brisbane;

I want to create a query from the Location page that looks at the taxonomy of this post and returns the video posts that have the same taxonomy.

In the above example, the "Video 1" video post would be returned and displayed on the location page.

1

1 Answers

3
votes

Good question, this is a little bit different than getting related categories or tags, though still uses a similar premise. There are a few ways you can do this, but one of the easiest may be to use a custom function that utilizes WP_Query. Add the following code to your functions.php file.

// Create a query for the custom taxonomy
function related_posts_by_taxonomy( $post_id, $taxonomy, $args=array() ) {
    $query = new WP_Query();
    $terms = wp_get_object_terms( $post_id, $taxonomy );

    // Make sure we have terms from the current post
    if ( count( $terms ) ) {
        $post_ids = get_objects_in_term( $terms[0]->term_id, $taxonomy );
        $post = get_post( $post_id );
        $post_type = get_post_type( $post );

        // Only search for the custom taxonomy on whichever post_type
        // we AREN'T currently on
        // This refers to the custom post_types you created so
        // make sure they are spelled/capitalized correctly
        if ( strcasecmp($post_type, 'locations') == 0 ) {
            $type = 'videos';
        } else {
            $type = 'locations';
        }

        $args = wp_parse_args( $args, array(
                'post_type' => $type,
                'post__in' => $post_ids,
                'taxonomy' => $taxonomy,
                'term' => $terms[0]->slug,
            ) );
        $query = new WP_Query( $args );
    }

    // Return our results in query form
    return $query;
}

Obviously you can change anything in this function to get the exact results you are looking for. Have a look at http://codex.wordpress.org/Class_Reference/WP_Query for further reference.

With that in place, you now have access to the related_posts_by_taxonomy() function, where you can pass in whichever taxonomy you want to find related posts for. So in your single.php or whichever template is being used for your custom post types, you can do something like the following:

<h4>Related Posts</h3>
<ul>
<?php $related =  related_posts_by_taxonomy( $post->ID, 'Video_Categories' );
    while ( $related->have_posts() ): $related->the_post(); ?>
        <li><?php the_title(); ?></li>
    <?php endwhile; ?>
</ul>