是的,完全有可能。
场景1如果只需要一个分类法,则只需获取分配给该分类法的所有术语的所有术语ID,然后将该术语ID数组传递给tax_query
(需要PHP5.4+)
$term_ids = get_terms(
\'TAXONOMY_NAME\',
[ // Array of arguments, see get_terms()
\'fields\' => \'ids\' // Get only term ids to make query lean
]
);
if ( $term_ids // Check if we have terms
&& !is_wp_error( $term_ids ) // Check for no WP_Error object
) {
$args = [
\'tax_query\' => [
[
\'taxonomy\' => \'TAXONOMY_NAME\',
\'terms\' => $term_ids,
]
],
];
$q = new WP_Query( $args );
// Run your loop as needed. Remeber wp_reset_postdata() after the query
}
如果您需要将术语分配给多个分类法的帖子,您可以在
SCENARIO 1$taxonomies = [\'TAXONOMY_NAME_1\', \'TAXONOMY_NAME_2\']; // Array of taxonomies, can also use get_taxonomies for all
$tax_array = []; // Set the variable to hold our tax query array values
foreach ( $taxonomies as $key=>$taxonomy ) {
$term_ids = get_terms(
$taxonomy,
[ // Array of arguments, see get_terms()
\'fields\' => \'ids\' // Get only term ids to make query lean
]
);
if ( $term_ids // Check if we have terms
&& !is_wp_error( $term_ids ) // Check for no WP_Error object
) {
$tax_array[$key] = [
\'taxonomy\' => $taxonomy,
\'terms\' => $term_ids,
];
}
}
$relation = \'OR\'; // Set the tax_query relation, we will use OR
if ( $tax_array ) { // Check if we have a valid array
if ( count( $tax_array ) == 1 ) {
$tax_query[] = $tax_array;
} else {
$tax_query = [
\'relation\' => $relation,
$tax_array
];
}
$args = [ // Set query arguments
\'tax_query\' => $tax_query,
// Any other query arguments that you might want to add
];
$q = new WP_Query( $args );
// Run your loop as needed. Remeber wp_reset_postdata() after the query
}