WP_QUERY用于将POST META分配给分类的帖子

时间:2017-08-23 作者:Stephen Sabatini

由于我们没有为此蒙皮或使用标准模板,我们遇到了一些困难,但我们在项目中走得太远,无法回头。我从来没有这样做过WP\\U查询,但有两件事可以概括它的结构。

我正在查询帖子类型tribe_events 其中postmeta密钥等于_VenueCity, _VenueState, 和_VenueCountry 和值等于GET/POST请求tribe_venue 分类法使我很难直接查询Posteta,因为它没有直接连接到tribe_events 岗位类型

<?php
    // Support for multiple locations to be filtered.
    $request_cities = array_map(function($item){return utf8_decode(urldecode($item));}, explode(\'+\', $_REQUEST[\'city\']));
    $request_states = array_map(function($item){return utf8_decode(urldecode($item));}, explode(\'+\', $_REQUEST[\'state\']));
    $request_countries = array_map(function($item){return utf8_decode(urldecode($item));}, explode(\'+\', $_REQUEST[\'country\']));

    // If the event only has 1 item convert to string.
    if (is_array($request_cities) && count($request_cities) <= 1) {
        $request_cities = implode("|", $request_cities);
    }
    if (is_array($request_states) && count($request_states) <= 1) {
        $request_states = implode("|", $request_states);
    }
    if (is_array($request_countries) && count($request_countries) <= 1) {
        $request_countries = implode("|", $request_countries);
    }
    $event_args = [];
    $event_args[\'meta_query\'] = [
        [
            \'key\' => \'_VenueCity\',
            \'value\' => $request_cities
        ], [
            \'key\' => \'_VenueState\',
            \'value\' => $request_states
        ], [
            \'key\' => \'_VenueCountry\',
            \'value\' => $request_countries
        ],
        \'relation\' => \'AND\'
    ];
    $event_args[\'tax_query\'] = [
        [
            \'taxonomy\' => \'tribe_venue\',
            \'field\' => \'term_id\',
            \'terms\' => $request_cities
        ]
    ];
    $events = tribe_get_events($event_args);
?>
如果有人能帮我弄明白这一点,我将不胜感激,因为我花了这么多时间试图弄明白这一点。

1 个回复
最合适的回答,由SO网友:Stephen Sabatini 整理而成

因此,在从活动日历的网站上进行了大量搜索之后,我找到了解决方案,并对其进行了修改以满足我的需要。代码如下所示:(注意,我还删除了数组到字符串的转换,因为它非常无意义。)

<?php
    // Support for multiple locations to be filtered.
    $request_cities = array_map(function($item){return utf8_decode(urldecode($item));}, explode(\'+\', $_REQUEST[\'city\']));
    $request_states = array_map(function($item){return utf8_decode(urldecode($item));}, explode(\'+\', $_REQUEST[\'state\']));
    $request_countries = array_map(function($item){return utf8_decode(urldecode($item));}, explode(\'+\', $_REQUEST[\'country\']));

    //Retrieve venues that match query criteria
    $args = [
        \'nopaging\' => true,
        \'post_type\' => \'tribe_venue\',
        \'meta_query\' => [
            [
                \'key\' => \'_VenueCity\',
                \'value\' => $request_cities,
                \'compare\' => \'IN\',
            ], [
                    \'key\' => \'_VenueState\',
                    \'value\' => $request_states,
                    \'compare\' => \'IN\',
            ], [
                    \'key\' => \'_VenueCountry\',
                    \'value\' => $request_countries,
                    \'compare\' => \'IN\',
            ],
            \'relation\' => \'AND\'
        ]
    ];
    $venues = get_posts($args);
    $venue_ids = wp_list_pluck($venues, \'ID\');
    wp_reset_postdata();

    $event_args[] = [
        \'meta_query\' => [
            [
                \'key\' => \'_EventVenueID\',
                \'value\' => $venue_ids,
                \'compare\' => \'IN\',
            ]
        ],
        \'paged\' => $paged
    ];
    $events = tribe_get_events($event_args);
?>
资料来源:https://theeventscalendar.com/support/forums/topic/events-list-with-filter-by-venue-in-custom-template/#post-1290489

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post