php 将post_type添加到get_terms args
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 将post_type添加到get_terms args相关的知识,希望对你有一定的参考价值。
/**
* Extend get terms with post type parameter.
*
* @global $wpdb
* @param string $clauses
* @param string $taxonomy
* @param array $args
* @return string
* @link https://dfactory.eu/wp-how-to-get-terms-post-type/
* Adds option for post_type to get_terms $args
*/
add_filter( 'terms_clauses', 'df_terms_clauses', 10, 3 );
function df_terms_clauses( $clauses, $taxonomy, $args ) {
if ( isset( $args['post_type'] ) && ! empty( $args['post_type'] ) && $args['fields'] !== 'count' ) {
global $wpdb;
$post_types = array();
if ( is_array( $args['post_type'] ) ) {
foreach ( $args['post_type'] as $cpt ) {
$post_types[] = "'" . $cpt . "'";
}
} else {
$post_types[] = "'" . $args['post_type'] . "'";
}
if ( ! empty( $post_types ) ) {
$clauses['fields'] = 'DISTINCT ' . str_replace( 'tt.*', 'tt.term_taxonomy_id, tt.taxonomy, tt.description, tt.parent', $clauses['fields'] ) . ', COUNT(p.post_type) AS count';
$clauses['join'] .= ' LEFT JOIN ' . $wpdb->term_relationships . ' AS r ON r.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN ' . $wpdb->posts . ' AS p ON p.ID = r.object_id';
$clauses['where'] .= ' AND (p.post_type IN (' . implode( ',', $post_types ) . ') OR p.post_type IS NULL)';
$clauses['orderby'] = 'GROUP BY t.term_id ' . $clauses['orderby'];
}
}
return $clauses;
}
以上是关于php 将post_type添加到get_terms args的主要内容,如果未能解决你的问题,请参考以下文章
php get_terms
按任何文章类型的自定义分类的术语列出文章
如何在涉及多个get_terms()的WordPress函数中返回多个值
wordpress调用指定类型post_type的文章
为 post_type = ?? 的每个 id 添加元值WordPress
更改 slug 时存档-post_type.php 中断