我一直在为此苦苦挣扎。我创建了一种自定义帖子类型,称为“ pt_initiatives”,事件日历创建的一种自定义帖子类型为“ tribe_events”。使用“高级自定义字段”,我向两个帖子类型都添加了一个关系字段,这也是双向的。关系字段称为“ bi_initiatives-events”。
我添加了带有过滤器栏的Events Calendar Pro。我正在尝试创建一个过滤器,以将pt_initiatives帖子显示为多复选框过滤器中的选项。 (这些职位只有9个)。到目前为止,我一直在工作。当选择了这些pt_initiatives之一时,我对如何更新事件列表有些迷惑。
我一直在使用wpdb查询,但没有成功。我不确定如何告诉它查看tribe_events帖子以查找哪些帖子具有共同点。我一直在搜索互联网,没有发现任何有用的信息,并且活动日历的人们似乎并不热衷于提供帮助,因为这被认为是“定制”。但是,它们提供了将过滤器作为功能添加的功能...关于如何执行此操作,他们的页面含糊不清,但我个人认为它没有太大帮助。
我对编写wpdb查询不太熟悉,因此您可以看到我添加了一个post循环来实现相同的目的。至少,我认为这与我遇到的一些示例的输出是相同的。
谢谢!
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' );