$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom post types - query multiple taxonomies|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - query multiple taxonomies

matteradmin10PV0评论

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,
        ),
    ),

));

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far