我正在尝试通过ACF的复选框字段来过滤我的自定义帖子类型。我使用本教程:https : //www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/
现在,我得到了一个问题,当ich筛选自定义帖子类型的存档页面上的复选框时,什么都不会改变。它仅生成正确的URL,但不过滤帖子。
有些人知道为什么吗?
function.php:
// array of filters (field key => field name)
$GLOBALS['my_query_filters'] = array(
'mitglieder' => 'mitglieder'
);
// action
function my_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ) return;
// bail early if not main query
// - allows custom code / plugins to continue working
if( !$query->is_main_query() ) return;
// get meta query
$meta_query = $query->get('meta_query');
// loop over filters
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
// continue if not found in url
if( empty($_GET[ $name ]) ) {
continue;
}
// get the value for this filter
// eg: http://www.website.com/events?city=melbourne,sydney
$value = explode(',', $_GET[ $name ]);
// append meta query
$meta_query = array(
array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
)
);
}
// update meta query
$query->set('meta_query', $meta_query );
}
守着一只汪