1

I have an extra taxonomy named Regions (like Categories).

For example my Categories are:

  • Villa
  • Housekeeper

And my Regions are:

  • Mykonos
  • Santorini
  • Crete

On single post page I want to show related posts by Region and that match a specific Category.

Example: On a Villa (Category) which is in Mykonos (Region) I would like to show on a row the Housekeepers (Category) that has Mykonos (Region) as a Region.

I guess that the code below will show the related by category posts. How it is possible to show only the posts that match both categories of a post?

<?php
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
 <ul>
        <li>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        <?php the_content('Read the rest of this entry &raquo;'); ?>
        </li>
 </ul> 
<?php }
wp_reset_postdata(); ?>

1 Answer 1

0

This seems a little tricky since you seem (ab)use native Category for things that aren't quite the same (Villa and Housekeeper). You will probably need to write a custom logic since WP won't just "get" the bit that if you are on one specific category (Villa) you want to query for another (Housekeeper).

As for querying itself WP by now has support for very elaborate taxonomy queries.

Loosely the part of query for it would something like this:

'tax_query' => array(
   'relation' => 'AND',
    array(
        'taxonomy' => 'category',
        'field'    => 'slug',
        'terms'    => 'housekeeper',
    ),
    array(
        'taxonomy' => 'region',
        'field'    => 'slug',
        'terms'    => 'mykonos',
    ),
),
1
  • Thank you for your answer. I will give that a try and let you know.
    – BigDropGR
    Commented Jul 6, 2016 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.