This code will get the related posts of the current custom-post-type single post being viewed. This will get posts only of the same custom post type and the same custom taxonomy term of the current single post. Use this code on single.php
, or on single-custom_post_type.php
. Use it to show a custom loop of related items on the sidebar or footer of the current single custom post.
Note: this code is only necessary for getting related posts by custom taxonomies. For a custom post type that supports regular categories, then do this instead: Get Related Posts For Custom Post Type By Category. Or, for regular posts, see Related Posts By Category.
<!-- begin custom related loop, isa --> <?php // get the custom post type's taxonomy terms $custom_taxterms = wp_get_object_terms( $post->ID, 'your_taxonomy', array('fields' => 'ids') ); // arguments $args = array( 'post_type' => 'your_custom_post_type', 'post_status' => 'publish', 'posts_per_page' => 3, // you may edit this number 'orderby' => 'rand', 'tax_query' => array( array( 'taxonomy' => 'your_taxonomy', 'field' => 'id', 'terms' => $custom_taxterms ) ), 'post__not_in' => array ($post->ID), ); $related_items = new WP_Query( $args ); // loop over query if ($related_items->have_posts()) : echo '<ul>'; while ( $related_items->have_posts() ) : $related_items->the_post(); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endwhile; echo '</ul>'; endif; // Reset Post Data wp_reset_postdata(); ?> <!-- end custom related loop, isa -->
For this loop to work for you, you must make the following changes to the code:
On line 7: change ‘your_taxonomy’ to the name of your custom taxonomy. Leave in the single quotes.
On line 10: change ‘your_custom_post_type’ to the name of your custom post type. Leave in the single quotes.
On line 12: optionally, change the number 3 to whatever number you need.
On line 16: change ‘your_taxonomy’ to the name of your custom taxonomy. Leave in the single quotes.
Optional: Change the display by changing the html on lines 26, 29, and 32. Add a thumbnail, or whatever.
funky
June 26th, 2012 at 11:50 am
Hi, thanks on your reply… I used your code here and it worked but I still have the same issue than with my code. the code show all posts from custom taxonomy “material”. And I need all the posts from the child taxonomy the single-custom is in. for example all the posts on “leather” or “plastic”
*** Original question ***
How would you change the code to use it on single-custom.php
with custom post type “bags”
and custom taxonomy:
1. material
1.1 Leather
1.2 Plastic
1.3 Paper
I need to show below the single-post all the other custom-post from the product taxonomy :S
Ej: Single-custom.php show bag with leather taxonomy.
I need to show below all the other custom-post with the same taxonomy
What i have so far:
Isabel
June 26th, 2012 at 1:15 pm
Oh, I misunderstood. I thought leather, plastic, and paper were the taxonomy terms of the ‘material’ taxonomy. I didn’t realize they were child taxonomies. I tested the code, and it works for my case, in which “leather, plastic, and paper” are terms of the taxonomy, not children. This code will then show only all the ‘leather’ or only ‘plastic’, etc.
But in your case, I can’t see why you would make them “child taxonomies.” Is there a reason why you made them child taxonomies, instead of just terms of the ‘material’ taxonomy?
funky
June 26th, 2012 at 1:34 pm
I’M SORRY YOU ARE RIGHT THEY ARE TERMS
BUT STILL HAVENT FOUND A WAY TO SET THE RELATED POSTS ONLY FROM THAT TERM.
THIS IS MY SINGLE-CUSTOM.PHP
the loop from your code print all post from the taxonomy Material. And I need to print if the single-custom post is showing a post from leather, all other post from leather…
funky
June 26th, 2012 at 1:45 pm
I got it. i just had to put after: $custom_taxterms[0] to set the current one….
thanks a lot for your help!
Isabel
June 26th, 2012 at 2:14 pm
🙂
Eric Young
March 7th, 2013 at 4:46 pm
Great snippet! Worked Great! This has been driving me crazy, so thanks Isabel.
Aurelio
June 13th, 2013 at 7:28 am
Very clear and easy to understand, with a little bit of customization works great for my needs. Thank you very much for sharing this.
Sarah
October 26th, 2013 at 9:15 am
This was perfect! Exactly what I was looking for.
Anderson
December 4th, 2013 at 12:29 pm
Dont work for me :((
Anderson
December 4th, 2013 at 1:48 pm
Now work for me
Tdude
January 6th, 2014 at 11:24 am
Hi!
It’s so easy when you know how to do it, right? THANKS A BUNCH FOR SHARING! Couldn’t find valid hints for this anywhere. This msg didn’t post first time I tried, maybe the session timed out. Checked the rest of your site meanwhile and had to go back and THANK YOU! Awesome tips. Will use some.
All the best from Stockholm,
T
Chris C
January 7th, 2014 at 3:21 pm
Thank you so much, this has been a huge help as I didn’t want to static-ly query an array of the existing terms in the taxonomy. Definitely makes it dynamic for the client, thanks again!
Isabela Sampaio
January 29th, 2014 at 12:54 pm
It works really well, thank you soooo much!
Is it possible to relate the posts with 2 or more taxonomies? I’ve tried replace the “your_taxonomy” part with “array(‘taxonomy1′,’taxonomy1’)” but it didn’t work 🙁
I’m not very familiar with php or wordpress, could you give me a hint?
Thanks! 🙂
Alexander
March 6th, 2014 at 1:08 pm
Thanks a lot! It works now!
I implemented the related products on this page: http://natige.kz/ru/products/biokefir-vitaminizirovannyjj
Albert
May 15th, 2014 at 7:01 am
Thank you! It’s fantastic!
Meghan
June 15th, 2014 at 7:53 pm
Works perfectly, THANK YOU! If only I’d found this page 3 hours ago!
Carlo
November 6th, 2014 at 5:32 am
Hi! Thank you so much for the code! It worked as intended. However, it did not display anything when I tried putting it in functions.php.
I put the function in single-mycustom.php like this:
And put your code in functions.php:
Thanks again! 🙂
Carlo
November 6th, 2014 at 5:39 am
Hello again. I was able to make it work by putting global $post; above your codes. Just posting it here in case some newbie like me encounter the same problem. 🙂
Isabel
November 6th, 2014 at 6:14 pm
Thank you. Yes, putting in a function like you did is a much cleaner solution. And, yes, you got it right, you need global $post in the function. 🙂
Breon
December 2nd, 2014 at 4:53 pm
Thanks for the great post. Is it possible to relate two or more taxonomies?
Isabel
December 4th, 2014 at 6:03 pm
Do you mean to relate 2 taxonomy terms, or 2 different taxonomies?
If you do want to relate 2 taxonomies, insert the following line under line 7 of the code above. Replace ‘second_taxonomy’ with the slug of your second taxonomy:
Then, replace the tax query (lines 14 through 20) with this:
Replace ‘your_taxonomy’ and ‘second_taxonomy’ in this snippet. This block replaces lines 14 through 20. This will show related posts that have matching terms in either of your two specified taxonomies. Hope that helps.
Bruno
July 21st, 2019 at 8:07 pm
How to make not by 1taxonomy AND 2taxonomy related posts. But how to make 1taxnomy OR 2taxonomy related posts? Not “AND”, but “OR”?
Isabel
August 8th, 2019 at 5:11 pm
To show posts from either one taxonomy or the other, make some empty lines at the beginning of line 23 (move the existing line 23 down to create empty lines). Then, insert this:
In this code snippet, be sure to change
your_taxonomy
to the name of your custom taxonomy on line 3.AND THEN, change lines 15–19 to just this:
That will change the relation to “OR” instead of “AND”.
Dean H
September 9th, 2015 at 11:35 pm
Thanks much for this Isabel. I’m trying to get 3 posts from the custom post type “cars”, with the taxonomy term “color”, and the taxonomy tag “red”. If I’m not mistaken, your code would get any post with the term “color” attached to it, not just the red ones, correct? If so, how do I edit it to be more specific?
Isabel
September 10th, 2015 at 12:45 pm
Hi. No, actually, it will get only the posts with the same color of whichever post is viewed at the time. So, on a post tagged with “red”, it will get other posts tagged with “red.” On lines 7 and 16, change ‘your_taxonomy‘ to ‘color‘, though. It gets confusing, but it will work that way to show only posts with the same color of the current post. I hope that helps.
Nitya
November 20th, 2015 at 12:35 pm
Thanks for this script. It’s saved my time and helped me a lot.
David
January 30th, 2016 at 4:27 pm
oh my good! thanks so much Isabel!
Cortney
March 13th, 2016 at 12:13 am
I’ll add my thanks — saved me loads of time! Only question:
I have a custom taxonomy that applies to BOTH “posts” and a custom post type (“podcast”). I want to use my custom taxonomy to display a mix of results from both post types. Would appreciate any tips!
Renee
October 13th, 2016 at 5:56 pm
Perfect! Thx.
Anderson
February 28th, 2017 at 9:35 am
This is a great!!! Thank you very much.
Joiner Leal
May 21st, 2017 at 1:28 pm
Thank you so much, this was so much helpfull to me, I really appreciate it.
Joiner.
Pangeran Kegelapan
October 10th, 2017 at 4:38 am
Thank you so much, it’s save my day. 😀
Anderson
November 8th, 2017 at 10:09 am
Thanks Isabel!
easonchiu
April 20th, 2018 at 6:49 am
Thank you so much!!!
looking for this answer for long time!!!
cssahu
May 28th, 2018 at 6:53 am
Code is working properly. Thanks for help
Amara wazir
November 12th, 2018 at 1:55 am
you saved my life. great 🙂
Jenny Rosenberger
March 1st, 2019 at 9:39 am
This worked for me! Thank you for sharing 🙂
Stephen Gunn
April 2nd, 2019 at 3:24 am
This worked perfect in 2019. Thank you.
George
May 30th, 2019 at 2:29 am
Nice example!
I would use get_the_category just for the performance. the wp_get_post_categories function does not use cache.
Nuno
May 18th, 2020 at 7:00 am
Thank you for sharing – if anyone needs here is an updated version of the code above https://gist.github.com/nfsarmento/dab9715819d91a0248933ee044d2bd51