【问题标题】:Get all terms from posts that have a certain post meta value从具有特定帖子元值的帖子中获取所有术语
【发布时间】:2022-06-29 00:13:48
【问题描述】:

我正在尝试从具有我使用 ACF 创建的 meta_key(performance_date_1) 和 meta_value(20220430) 的自定义帖子类型(艺术家)中获取所有术语。

这是我迄今为止尝试过的。

$events = get_terms( 'artist_event', array(
    'post_type' => 'artist',
    'taxonomy' => 'artist_event',
    'hide_empty' => false,
    'suppress_filters' => false,
    'meta_query' => array(
        // 'relation' => 'OR',
        array(
            'key' => 'performance_date_1',
            'compare' => '=',
            'value' => '20220430',
        )
    ),
) );

当使用get_terms 时,meta_query 是在分类字段还是帖子字段中查找?我需要元查询来查找帖子字段以解决我认为的这个问题。

【问题讨论】:

标签: wordpress


【解决方案1】:

我也遇到过类似的情况。就我而言,我通过WP_Query::have_posts 使用array_filter 来处理帖子,尽管这肯定不是最高效的解决方案。

$events = array_filter(
    get_terms(array('taxonomy' => 'artist_event')), 
    function($term) {
        return (new WP_Query(array(
            'post_type' => 'artist',
            'posts_per_page' => -1,
            'tax_query' => array(
                'taxonomy' => 'artist_event', 
                'field' => 'slug', 
                'terms' => $term->slug,
            ),
            'meta_query' => array(
                array(
                    'key' => 'performance_date_1',
                    'compare' => '=',
                    'value' => '20220430',
                )
            ),
        )))->have_posts();
    }
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-18
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 2015-08-22
    相关资源
    最近更新 更多