Not sure how hard this is but i'll try and explain it as simply as possible.

I have a custom post type called: "Movies" with an taxonomy term called "Actor"

For example purposes suppose were on the taxonomy page "Tom Cruise" which will output a load of posts(movies) that Tom has been in.

What i'm trying to work out is how i can get a related terms(Actors) loop going so i can have this in the sidebar on the Actor terms page.

So for example, Tom Cruise's taxonomy will be in the post(Movie) "Tropic Thunder", but so will "Ben Stiller" and others. So on the Tom Cruise term page under related actors i'd like it to display "Ben Stiller" with a link to his term page (and vise versa).

  • Is Tropic Thunder stored as a post with Tom Cruise as a category, or is it the other way around? – mor7ifer Jan 11 '12 at 11:31
  • Tropic Thunder would be a post in the post type "Movies", and Tom Crusise would be a Taxonomy linked to the post type "Movies". Hope that makes sense, thx – Carpy Jan 11 '12 at 11:48

Just banged this out real quick, it should be a start at least. The basic concept here was to get ALL the posts tagged with the main person (in this case, Tom Cruise, you'll have to setup that variable on your own. Once you have those, iterate through them, and foreach movie (Top Gun, Tropic Thunder, Mission Impossible, etc), get the other actors for that movie. From there you generate an array containing the actor slugs (though you could use IDs or anything else in the object, really, suit it to your needs) as keys and the relevance (only crawling out one level) as the value. Then you do an arsort to get it with the most relevant up top, which will allow you to slap a for( $i=0; $i<5; $i++) on there and do the boring HTML and stuff.

$wp_query = new WP_Query;
$args = array(
    // post basics
    'post_type'      => 'Movies',  // check capitalization, make sure this matches your post type slug
    'post_status'    => 'publish', // you may not need this line.
    'posts_per_page' => 10,        // set this yourself, 10 is a placeholder
    // taxonomy
    'tax_query'      => array(
            'taxonomy' => 'actors', // slug for desired tag goes here
            'field'    => 'slug',
            'terms'    => 'Tom Cruise', // should work without a slug, try it both ways...and use a variable, don't hardcode

$results = $wp_query->query( $args );

$related = array();

if( count( $results ) > 0 ) {
    foreach( $results as $result ) {
        $new_related = get_the_terms( $result->ID, 'actors' ); //get terms for this post
        if( is_array( $new_related ) ) {
            foreach( $new_related as $v ) {
                $name = $v->name;
                if( array_key_exists( $name, $related ) )
                    $related[$name]++; // add to total if it's already there
                    $related[$name] = 1; // initialize if it does not
    arsort( $related, SORT_NUMERIC );
  • I'm sure this is a good answer, but tbh it's a bit over my head. The "terms" part in the array would be the tax page id i assume? – Carpy Jan 12 '12 at 14:58
  • Yes, my apologies, I forgot to include documentation. It uses the WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query) class (which itself actually uses the [WP_Tax_Query class, which is poorly documented, unfortunately). If you change out the "Tom Cruise" for the slug of the tag the page is for, the code will get all the movies tagged 'Tom Cruise', then get all the tags for all those movies (tags being actors) and decide which is most relevant by using number of instances. – mor7ifer Jan 12 '12 at 15:04

You need to create a loop that's based off the tax.

$args = array(
    'actor' => get_query_var('actor')
    'post_type' => 'movies',
    'posts_per_page' => 5
$loop = new WP_Query($args);

       //Do something

This is really a proof of concept. You will have to tweak it to your own needs. I would suggest that you print recursive on $wp_query to find what query variables are being set for your custom post type and taxes.

global $wp_query;

This should help you tweak the custom loop to fit your needs.

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.