Basically, I'm looking to query all post of category 'news', and any combination of other categories to display as Related News on single content type pages.

So, if someone writes a post, and categorizes it as 'News', and 'Stackable', then when the a 'Stackable' single item is viewed, the 'News' post with the same category 'Stackable' will appear.

I'm aware of the category__in, but that will display any post of any of the queried categories.
I'm aware of the category__and, but that will only display posts with ALL the queried categories.

I want it to only display posts that are 'News', or id=7, and any related category, so a combination of both __in __and.
Regular 'News' posts should not show up.

Another way to explain is to list this.
Say we have a 'Stackable' content item:
Then any 'News' posts with these category combinations should show up:

  • News, and Stackable
  • News, and Stackable, and Generic
  • News, and Stackable, and Poster
  • News, and Stackable, and Generic, and Map

Say we have a 'Containable' content item:
Then any 'News' posts with these category combinations should show up:

  • News, and Containable
  • News, and Containable, and Map
  • News, and Containable, and Dumpster
  • News, and Containable, and Generic, and Dice

These would not show anywhere except the News page.

  • News
  • News, and Map
  • News, and Generic, and Poster
  • News, and Dumpster, and Dice, and Map

Any help would be much appreciated.

$categories = wp_get_post_categories( $post->ID ); array_push( $categories, 7 ); 
// $related = get_posts( array( 'category__in' => $categories, 'numberposts' => 3, 'post__not_in' => array( $post->ID ) ) ); 
$related = get_posts( array( 'category__and' => $categories, 'numberposts' => 3, 'post__not_in' => array( $post->ID ) ) ); 
if( $related ) foreach( $related as $post ) { 
  setup_postdata( $post ); 
  • 1
    What have you coded so far yourself? Please include your work and why it doesn't work. – Brad Dalton Jul 6 '15 at 20:47
  • I've tried both in/and with no luck for this case. I've quickly coded up an example above. codex.wordpress.org/Class_Reference/… – Ganginator Jul 6 '15 at 21:06
  • I do not understand the last part of your question: These would not show anywhere except the News page – Pieter Goosen Jul 7 '15 at 5:32
  • So if someone categorizes a post as a 'News' post, but not as a content type like 'Widget', then that post is just on the News page. Which I got running just fine. But then I have categories of say a 'Widget', like 'Stackable', or something. So I want to simply show the top three related posts that are absolutely 'News', and then 'Stackable' on the Stackable category page, or absolutely 'News', and then 'Compact' on the Compact category page, and then any combination of categories that may be related, say 'Foldable'. I want to accomplish this with categories. Any help is much appreciated. – Ganginator Jul 7 '15 at 7:21
  • How many categories can a post have. Just a tip, if you reply to my comments, use the @ sign in front of my username. This way, I get "pinged" so I know I have a message. Without that, your message lands on deaf ears. I accidentally stumpled back to this question and only then saw the new message :-) – Pieter Goosen Jul 7 '15 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.