首页 > 解决方案 > Wordpress:事件日历,ACF:基于 ACF 关系字段的 TEC 过滤条过滤器

问题描述

我一直在努力解决这个问题。我创建了一种自定义帖子类型,称为“pt_initiatives”,而事件日历创建的一种自定义帖子类型称为“tribe_events”。使用高级自定义字段,我为两种帖子类型添加了一个关系字段,这也是双向的。关系字段称为“bi_initiatives-events”。

我有带有过滤器栏的事件日历专业版。我正在尝试创建一个过滤器,将 pt_initiatives 帖子显示为多复选框过滤器中的选项。(这些帖子只有 9 个)。到目前为止,我有那部分工作。I am a bit lost on how to update the list of events when one of these pt_initiatives are chosen.

我一直在玩 wpdb 查询但没有成功。我不知道如何告诉它查看tribe_events 帖子以找出哪些帖子有共同之处。我一直在网上搜索,但没有发现任何有用的东西,而且活动日历的人似乎不太热衷于提供帮助,因为这被认为是“定制”。但是,他们提供了将过滤器添加为功能的能力......他们有一个关于如何做到这一点的非常模糊的页面,但我个人认为它没有太大帮助。

我对编写 wpdb 查询不太熟悉,因此您可以看到我添加了一个后循环来实现相同的目的。至少,我认为它的输出与我遇到的一些示例相同。

提前致谢!

   class Tribe__Events__Filterbar__Filters__Initiative extends Tribe__Events__Filterbar__Filter {
    public $type = 'checkbox';

    public function get_admin_form() {
        $title = $this->get_title_field();
        $type = $this->get_multichoice_type_field();
        return $title.$type;
    }

    protected function get_values() {
        /** @var wpdb $wpdb */
        global $wpdb;

        // get initiative IDs associated with published posts
        // $initiative_ids = $wpdb->get_col( 
        //     $wpdb->prepare( 
        //         "SELECT DISTINCT m.meta_value 
        //         FROM {$wpdb->postmeta} m 
        //         INNER JOIN {$wpdb->posts} p 
        //         ON p.ID=m.post_id 
        //         WHERE p.post_type=%s,
        //         AND p.post_status='publish' 
        //         AND m.meta_key='bi_initiatives-events' 
        //         AND m.meta_value > 0", $post_type
        //     ) 
        // );


        $initiative_ids = array();
        // WP_Query arguments
        $args = array(
            'post_type' => array( 'pt_initiatives' ),
            'posts_per_page' => -1,
            'meta_query' => array(
                array(
                    'key' => 'bi_initiatives-events',
                    // 'value' => '',
                    'compare' => 'EXISTS'
                )
            )
        );

        $query = new WP_Query( $args );

        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();

                $initiative_ids[] = get_the_ID();
            }
        } 
        wp_reset_postdata();

        array_filter( $initiative_ids );
        if ( empty( $initiative_ids ) ) {
            return array();
        }

        /**
         * Filter Total Initiatives in Filter Bar
         * Use this with caution, this will load initiatives on the front-end, may be slow
         * The base limit is 200 for safety reasons
         *
         *
         * @parm int  200 posts per page limit
         * @parm array $initiative_ids   ids of initiatives attached to events
         */
        $limit = apply_filters( 'tribe_events_filter_bar_pt_initiatives_limit', 200, $initiative_ids );

        $initiatives = get_posts( array(
            'post_type' => 'pt_initiatives',
            'posts_per_page' => $limit,
            'suppress_filters' => false,
            'post__in' => $initiative_ids,
            'post_status' => 'publish',
            'orderby' => 'title',
            'order' => 'ASC',
        ) );

        $initiatives_array = array();
        foreach ( $initiatives as $initiative => $value ) {
            $initiatives_array[] = array(
                'name' => $value->post_title,
                'value' => $value->ID,
            );
        }
        return $initiatives_array;
    }

    protected function setup_join_clause() {
        add_filter( 'posts_join', array( 'Tribe__Events__Query', 'posts_join' ), 10, 2 );
        global $wpdb;

        $this->joinClause .= " LEFT JOIN {$wpdb->postmeta} AS initiatives_filter ON ({$wpdb->posts}.ID = initiatives_filter.post_id AND initiatives_filter.meta_key = 'bi_initiatives-events')";
    }

    protected function setup_where_clause() {
        if ( is_array( $this->currentValue ) ) {
            $initiative_ids = implode( ',', array_map( 'intval', $this->currentValue ) );
        } else {
            $initiative_ids = esc_attr( $this->currentValue );
        }

        $this->whereClause = " AND initiatives_filter.meta_value IN ($initiative_ids) ";

    }
}
new Tribe__Events__Filterbar__Filters__Initiative( __( 'Initiatives', 'tribe-events-filter-view' ), 'initiatives_filter' );

标签: wordpressadvanced-custom-fields

解决方案


推荐阅读