custom post types - query multiple taxonomies

admin2025-06-07  1

I have a function set up to show 'Similar Products', i.e. showing products that share the same products-category taxonomy term. This works great but I want to narrow the function even further and make it more specific. Thus I want it to look at 2 taxonomies (product-category and space) to find similar products. The current markup as follows:

    <?php  
    $terms = wp_get_post_terms( $post->ID, 'products-category' );
    if($terms){
      // post has course_type terms attached
      $course_terms = array();
      foreach ($terms as $term){
       $course_terms[] = $term->slug;
      }

     $original_query = $wp_query;
     $wp_query = null;
     $wp_query = new WP_Query(
        array(
            'posts_per_page' => '4',
            'post_type' => 'regularproducts',
            'tax_query' => array(
                array(
                    'taxonomy' => 'products-category',
                    'field' => 'slug',
                    'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
                ),
            ),
            'orderby' => 'title',
            'order' => 'ASC',
        )
    );

    if ( have_posts() ): ?>

    <?php while (have_posts() ) : the_post(); ?> //etc...

So it currently only looks at the products-category taxonomy for similar products (posts), but I want it to look at BOTH product-category and space to display more specific similar products, if possible. Any suggestions would be greatly appreciated!

I have a function set up to show 'Similar Products', i.e. showing products that share the same products-category taxonomy term. This works great but I want to narrow the function even further and make it more specific. Thus I want it to look at 2 taxonomies (product-category and space) to find similar products. The current markup as follows:

    <?php  
    $terms = wp_get_post_terms( $post->ID, 'products-category' );
    if($terms){
      // post has course_type terms attached
      $course_terms = array();
      foreach ($terms as $term){
       $course_terms[] = $term->slug;
      }

     $original_query = $wp_query;
     $wp_query = null;
     $wp_query = new WP_Query(
        array(
            'posts_per_page' => '4',
            'post_type' => 'regularproducts',
            'tax_query' => array(
                array(
                    'taxonomy' => 'products-category',
                    'field' => 'slug',
                    'terms' => $course_terms, //the taxonomy terms I'd like to dynamically query
                ),
            ),
            'orderby' => 'title',
            'order' => 'ASC',
        )
    );

    if ( have_posts() ): ?>

    <?php while (have_posts() ) : the_post(); ?> //etc...

So it currently only looks at the products-category taxonomy for similar products (posts), but I want it to look at BOTH product-category and space to display more specific similar products, if possible. Any suggestions would be greatly appreciated!

Share Improve this question asked Nov 9, 2014 at 21:09 user1374796user1374796 39511 gold badges18 silver badges30 bronze badges 4
  • Add another array in 'tax_query' => array( this should solve your problem. – SLH Commented Nov 9, 2014 at 21:13
  • @SLH yes but what about $terms = wp_get_post_terms( $post->ID, 'products-category' ); as this is only using the product-category taxonomy term? – user1374796 Commented Nov 9, 2014 at 21:29
  • Then create other variable $space_terms and get the terms by post ID for your second taxonomy. array( 'taxonomy' => 'space', 'field' => 'slug', 'terms' => $space_terms, ), – SLH Commented Nov 9, 2014 at 21:32
  • @SLH ok, unless its something I'm doing wrong...would you possibly be able to put the full code in an answer so I can test? – user1374796 Commented Nov 9, 2014 at 21:51
Add a comment  | 

2 Answers 2

Reset to default 16

First of all, get all term slugs from the custom taxonomy space by current post ID.

$space_terms = wp_get_post_terms( $post->ID, 'space' );
if( $space_terms ) {
  $space_terms = array();
  foreach( $space_terms as $term ) {
   $space_terms[] = $term->slug;
  }
}

You should specify the logical relationship between each inner taxonomy array when there is more than one.

The relation key in the array describes the relationship. Possible values are OR and AND.

'tax_query' => array(
    'relation' => 'OR',
    array(
        'taxonomy' => 'products-category',
        'field'    => 'slug',
        'terms'    => $course_terms,
    ),
    array(
        'taxonomy' => 'space',
        'field'    => 'slug',
        'terms'    => $space_terms,
    ),
),

Simply get all terms first: Here i used property custom post type for example

$property_statu_terms = wp_get_post_terms( $post->ID, 'property_status', array(  'fields' => 'ids' ));
$property_type_terms = wp_get_post_terms( $post->ID, 'property_type', array(  'fields' => 'ids' ));
$property_city_terms = wp_get_post_terms( $post->ID, 'property_city', array(  'fields' => 'ids' ));
$property_state_terms = wp_get_post_terms( $post->ID, 'property_state', array(  'fields' => 'ids' ));
$property_feature_terms = wp_get_post_terms( $post->ID, 'property_feature', array(  'fields' => 'ids' ));

Use relation [OR] or [AND] to get your desired posts.

$query = new WP_Query( array(

    'post_type'             => 'property',
    'posts_per_page'        => 3,
    'orderby'               => 'rand',
    'post__not_in'          => array( $post->ID ),
    'ignore_sticky_posts'   => 1,
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'property_status',
            'field'    => 'id',
            'terms'    => $property_statu_terms,
        ),
        array(
            'taxonomy' => 'property_type',
            'field'    => 'id',
            'terms'    => $property_type_terms,
        ),
        array(
            'taxonomy' => 'property_city',
            'field'    => 'id',
            'terms'    => $property_city_terms,
        ),
        array(
            'taxonomy' => 'property_state',
            'field'    => 'id',
            'terms'    => $property_state_terms,
        ),
        array(
            'taxonomy' => 'property_feature',
            'field'    => 'id',
            'terms'    => $property_feature_terms,
        ),
    ),

));
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749228029a317433.html

最新回复(0)