$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 taxonomy - Query Multiple Taxonomies and Multiple Terms with Different Operators|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 taxonomy - Query Multiple Taxonomies and Multiple Terms with Different Operators

matteradmin11PV0评论

I am attempting to find related custom post types that share at least one term in taxonomy A and at least one term in taxonomy B. So it is sort of taxonomy A AND taxonomy B but OR within the terms of these taxonomies. What I have below brings 0 results, even though it should.

"tax_query" => array(
              'relation' => 'AND',
                array(
                   'taxonomy' => 'values',
                   'field' => 'term_id',
                   'terms' => $values_array
                ),
                array(
                    'taxonomy' => 'product_type',
                    'field' => 'term_id',
                    'terms' => $product_type_array
                )

      )

This here below where I've added "or" pulls all posts of that type regardless of taxonomy terms:

"tax_query" => array(
              'relation' => 'AND',
                array(
                   'taxonomy' => 'values',
                   'field' => 'term_id',
                   'terms' => $values_array,
                    'operator' => 'OR'
                ),
                array(
                    'taxonomy' => 'product_type',
                    'field' => 'term_id',
                    'terms' => $product_type_array,
                    'operator' => 'OR'
                )

      )

EDIT: was asked for more context on how the $values_array and $product_type_array were built. Below is what happens prior to the query to build these:

$post = get_post();
$post_type = get_post()->post_type;
$post_id = get_post()->ID;
$values = wp_get_post_terms($post_id, 'values');
$product_types = wp_get_post_terms($post_id, 'product_type');

$product_type_array = [];

foreach($product_types as $product_type){
    array_push($product_type_array, $product_type->term_id);
}

$values_array = [];

foreach($values as $value){
    array_push($values_array, $value->term_id);
}
Post a comment

comment list (0)

  1. No comments so far