不幸的是,似乎没有办法抓住术语;“如”;名字或鼻涕虫。请参见Taxonomy Parameters in WP_Query. 但我们可以将其分解为两个查询。
通过部分段塞匹配获取术语ID查询分配给这些术语ID的帖子我们可以使用WPDB或get_terms()
. 我更喜欢WPDB,因为get_terms()
没有slug__like
参数,但aname__like
. 如果可以的话,那么调用将如下所示:
$terms_ids = get_terms( array(
\'taxonomy\' => \'store_number\',
\'fields\' => \'ids\',
\'hide_empty\' => false,
\'name__like\' => \'80\',
) );
如果我们想通过slug专门抓取,可以使用以下函数:
/**
* Grab term IDs by partial slug match
*
* @param String $partial
* @param String $taxonomy
*
* @return Array $term_ids
*/
function wpse378026_term_ids_by_partial( $partial, $taxonomy ) {
global $wpdb;
$slug_like = $wpdb->esc_like( $partial ) . \'%\';
$term_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT t.term_id
FROM {$wpdb->terms} AS t
INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id
WHERE t.slug LIKE %s AND
tt.taxonomy = %s
",
$slug_like,
$taxonomy
) );
return $term_ids;
}
// Function call
$term_ids = wpse378026_term_ids_by_partial( \'80\', \'store_number\' );
然后,我们可以在WP\\U查询中使用术语ID,如下所示:
$stores = new WP_Query( array(
\'post_type\' => \'tax-services\',
\'order\' => \'DESC\',
\'posts_per_page\' => -1,
\'tax_query\' => array( array(
\'taxonomy\' => \'store_number\',
\'field\' => \'term_id\',
\'terms\' => $term_ids,
\'operator\' => \'IN\',
) )
) );