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 category of the current single post. Obviously, this only works for your custom post type if you gave it support for ‘category’ when you registered it.
Use this code on the sidebar of 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 category of custom post types. For related posts by category of regular posts, see Related Posts By Category. Or, for related posts by custom taxonomy, rather than category, see Get Related Posts For Custom Post Type by Custom Taxonomy.
<!-- begin custom related loop, isa --> <?php // get the custom post type's taxonomy terms $custom_taxterms = wp_get_object_terms( $post->ID, 'category', array('fields' => 'ids') ); $args = array( 'post_type' => 'YOUR_CUSTOM_POST_TYPE', 'post_status' => 'publish', 'posts_per_page' => 3, // you may edit this number 'orderby' => 'rand', 'post__not_in' => array ( $post->ID ), 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'id', 'terms' => $custom_taxterms ) ) ); $related_items = new WP_Query( $args ); // loop over query if ( $related_items->have_posts() ) : ?> <li class="widget widget_categories"> <h3 class="widget-title">Similar Posts</h3> <ul> <?php while ( $related_items->have_posts() ) : $related_items->the_post(); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul> </li> <?php endif; // Reset Post Data wp_reset_postdata(); ?> <!-- end custom related loop, isa -->
For this loop to work for you, you must make the following change to the code:
On line 9: change YOUR_CUSTOM_POST_TYPE
to the name of you custom post type. Leave in the single quotes.
You may optionally edit the HTML on lines 26–28, 32, 36, and 37. If left as is, it will show a list of 3 related posts. To show a different number of posts, you can change the “3” on line 11 to whatever number you need.
Agus Wikardi
November 19th, 2012 at 12:51 am
Thanks for share isabel
Helene
February 11th, 2013 at 3:38 pm
Hello,
I’m french so excuse me for my very bad english !!!
Thanks for this code, it’s very usefull for me !
I just have one problem, the instruction ‘orderby’ doesn’t work, i try ‘ASC’, ‘DESC’, ‘ID’… but it’s always by DESC…
Do you have an idea for me ?
thank you very much from France !
Isabel
February 11th, 2013 at 4:45 pm
Use ‘order‘, not ‘orderby’ to specify ‘ASC’ or ‘DESC’.
‘Orderby’ is to specify whether it will be by one of the following:
‘ID’
‘author’
‘title’
‘name’
‘date’
‘rand’, etc..
Then ‘order’ either ‘ASC’ or ‘DESC’.
Example:
Another:
Merci pour votre question. 🙂
Helene
February 12th, 2013 at 1:45 am
thanks for your reply
i try this but it’s doesn’t work :
$args = array(
‘post_type’ => ‘portfolio’,
‘posts_per_page’ => -1,
‘order’ => ‘date’,
‘orderby’ => ‘ASC’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘albums’,
‘field’ => ‘id’,
‘terms’ => $custom_taxterms
)
)
);
Isabel
February 12th, 2013 at 9:36 am
You have ‘order’ and ‘orderby’ mixed up. You have:
‘order’ => ‘date’,
‘orderby’ => ‘ASC’,
Switch it to:
Pingback: Get Related Posts For Custom Post Type by Custom Taxonomy
Daniel Viera
May 15th, 2013 at 6:17 pm
Great tool.
I need to add pagination to the related post
previous_posts_link();
next_posts_link();
But can make it work.
Any advice?
Thanks in advance
Daniel
Isabel
May 15th, 2013 at 7:19 pm
Thank you. If you mean on a single post, then use
previous_post_link();
next_post_link();
Almost the same thing you have, but without the ‘s’ at the end of `post`. Hope it helps.
Daniel Viera
May 15th, 2013 at 7:26 pm
Thanks Isabel for your fast response.
But I don’t need the prev or next link for the single post.
I have a lot of related posts, and I want to display only 9 of them per page, but be able to navigate to the next, or previous set of 9 “related post”, while viewing a single post.
I hope I could explain well, excuse my english, I speak spanish.
Best Regards
Daniel
Isabel
May 15th, 2013 at 10:53 pm
Oh, okay. That would involve some extra coding. (no time right now, sorry) I would take a look at this plugin: Related Posts Slider. It seems to do what you’re looking for. Hope this helps.
Daniel Viera
May 16th, 2013 at 6:25 am
Thanks u very much!
Carlos
July 5th, 2013 at 9:15 am
Hi Isabel. I tried one of your other codes for getting related custom posts with custom taxonomy and it worked perfectly. Now I’d like to get all the custom posts regardless of it’s taxonomy, so I think this code might be the way to go. However, I don’t know what you mean by “Obviously, this only works for your custom post type if you gave it support for ‘category’ when you registered it”
Thanks for the awesome codes and your work.
Isabel
July 5th, 2013 at 3:14 pm
Your welcome. If you want all posts of a custom post type, this is probably isn’t what you need, because this filters them by the post type’s category. This code only works if you assign categories to the custom posts.
So to answer your question:
When you register the post type, which should look something like this:
You must add this to the array of arguments in the $args:
That will let you assign categories to your custom post types.
Carlos
July 7th, 2013 at 6:07 pm
Well, in my case I only have one custom type post called projects. So maybe I don´t even need to create a category,
right? I used your code in https://isabelcastillo.com/related-custom-post-type-taxonomy to retrieve the thumbnails of all the related posts at the end of each post. Now I was looking to remove the filter and display all the thumbnails from the custom post type called “projects” regardless of taxonomy.
Isabel
July 8th, 2013 at 2:02 am
Right, you don’t need to filter by cat or taxonomy. This should work:
This gets all posts of the “projects” post type:
Anderson Multimedia
July 10th, 2013 at 11:51 am
I’ve tried this, and it looks like a neat, clean solution. But it doesn’t seem to be filtering by term for me. Any advice?
Anderson Multimedia
July 10th, 2013 at 12:10 pm
I used this solution instead. http://stackoverflow.com/questions/14592198/related-posts-in-wordpress-excluding-current-post
Dwaynne
September 29th, 2014 at 3:49 am
Hi, Isabel. Is there a way to use this for a page? I have a page that has the same slug as the category slug. I want to be able to use the page’s slug to pull in all posts from that specific category.
Isabel
September 29th, 2014 at 11:18 am
For that, you’d have to create a custom page template for that page. You can do this by copying your theme’s page.php into a new file. Name it page-slug.php in which “slug” is the page’s slug. Paste this header at the very top of it:
See this for more info on custom page templates.
Then, to display only posts that have your desired category, first find the loop, which begins with something like this:
and ends with something like this:
And replace the entire loop with this:
Replace the 13, on line 13 above, with your desired category ID.
Hope that helps.
Dwaynne
September 29th, 2014 at 11:25 am
Thanks a mil. I did some more searching on Google after I emailed you and found this solution, which fits my situation exactly:
http://wordpress.stackexchange.com/questions/127189/filtering-posts-by-category-name-based-on-pages-slug
Thanks again for taking the time to assist!
Max
August 24th, 2015 at 10:57 pm
Just noticed why it wont show on my site. when i replaced tax_query to wp_query it shows, still the issue is how to exclude the post on what i am?
Max
August 24th, 2015 at 11:07 pm
Actually it works as well with tax_query if i change the first letter to the capital one, so Tax_query, but still the post appers, really weird, waiting for some solution.Cheers
Max
August 24th, 2015 at 11:18 pm
Found a solution. If anyone is having the same issues (under related post it appears the one on which you are) move out
from Tax_query and place it above under $args
Nat
March 8th, 2017 at 12:16 pm
How can I add this as a function? Just copy and paste?
Isabel
March 9th, 2017 at 1:05 am
You can use it as a function by copying lines 6–41 and paste theme here into line 5:
And then you would display the related posts by using this template tag inside a theme (or child theme’s) template file:
Anthony
April 14th, 2017 at 12:48 pm
Hello, I am using your code to display Jury members based on the year that is selected (juryyear). Within that year it will list all jury members with their categories assign to them jury_category. Is there away I can sort jury_category alphabetically? As of right now it is listing the category based off the jury order.
Isabel
April 17th, 2017 at 2:27 pm
You can do this by changing your line 25 from
'orderby' => 'title',
to'orderby' => 'name',
. This should work since the terms use “name” instead of “title” for the title field. Hope that helps.