A client of mine has custom post types that they set up using a plugin rather than defining them in the functions.php. (So, YARPP for the solution won’t work). In this custom post types, they have various categories. Each post belongs to one or more categories. After each post, they would like to display 5 posts that are in ALL the same categories (could be one or many categories).
So, what I need to do is set up a way to dynamically find all the categories a post belongs to (I’ve done that & have it set up as an echo right now until I work this out), then query to find 5 posts that belong to ALL the same categories (either they belong to all of them or they shouldn’t be displayed).
Here’s the code I’ve come up with – but, I can’t figure out how to “combine them” so it not only gets the terms (categories) but also forces the query to use those terms to filter the 5 “related” programs.
Both parts of the code work as coded but I need to combine the two so when it queries the posts in the second part of the code, the ones selected match ALL categories that the post was in (which are being echo’d in the first bit of code to see if would actually give me the terms).
Right now, the second part of the code is returning results that are in any of the categories that the post is in – so, it may be in a top level category (and thus, not really related) but still be returned. I need it to return posts that are filed with ALL the categories/terms that the current post has. If a post is not in all the same categories (exact match for ALL categories of current post), it should not be returned. (For example, if a post is in the parent “North America” category, but, not any of the other categories that the post is in, it should NOT be returned in the results).
- The topic ‘Displaying related posts that match all categories of current post’ is closed to new replies.