我正在为我的自定义帖子类型添加自定义管理员筛选器。我有点搞不清楚该怎么做parse_query
筛选,以便根据2个或更多筛选器筛选我的帖子。
最初,对于1过滤器,我可以执行以下操作:
$query->query_vars[\'meta_key\'] = \'city\';
$query->query_vars[\'meta_value\'] = $cityId;
$query->query_vars[\'meta_compare\'] = \'=\';
我会得到我的过滤结果。
我想知道,WP是否会接受更多查询变量的meta\\u键/值。。。
我正试图设定一个meta_query
下面是我所做的,但它不起作用。
add_filter( \'parse_query\', \'wpr_manager_filter\' );
function wpr_manager_filter($query) {
global $page;
$current_page = isset( $_GET[\'post_type\'] ) ? $_GET[\'post_type\'] : \'\';
if ( is_admin()
&& \'properties\' == $current_page
&& \'edit.php\' == $page
)
{
$metaQuery = array();
if (isset( $_GET[\'city-filter\'] ) && $_GET[\'city-filter\'] != \'-1\')
{
$cityId = (int)$_GET[\'city-filter\'];
$metaQuery[\'relation\'] = \'AND\';
$metaQuery[] = array(
\'meta_key\' => \'city\',
\'meta_value\' => $cityId,
\'meta_compare\' => \'=\'
);
// $query->query_vars[\'meta_key\'] = \'city\';
// $query->query_vars[\'meta_value\'] = $cityId;
// $query->query_vars[\'meta_compare\'] = \'=\';
}
if (isset( $_GET[\'visibility-filter\'] ) && $_GET[\'visibility-filter\'] != \'-1\')
{
$visibility = (int)$_GET[\'visibility-filter\'];
$metaQuery[2] = array(
\'meta_key\' => \'visibility\',
\'meta_value\' => $visibility,
\'meta_compare\' => \'=\'
);
}
$query->meta_query = $metaQuery;
}
}
这是漫长的一天,我刚刚开始更加熟悉更深入的WP概念,因此任何能推动我前进并解释我做错了什么的帮助都将不胜感激。
最合适的回答,由SO网友:FFrewin 整理而成
这是我的最后一段代码:
function wpr_manager_filter($query) {
global $pagenow;
global $typenow;
$current_page = isset( $_GET[\'post_type\'] ) ? $_GET[\'post_type\'] : \'\';
if ( is_admin()
&& \'properties\' == $typenow
&& \'edit.php\' == $pagenow
)
{
$queryParamsCounter = 0;
if (isset( $_GET[\'city-filter\'] ) && $_GET[\'city-filter\'] != \'-1\')
{
$cityId = (int)$_GET[\'city-filter\'];
$queryParamsCounter++;
}
if (isset( $_GET[\'visibility-filter\'] ) && $_GET[\'visibility-filter\'] != \'-1\')
{
$queryParamsCounter++;
$visibility = (int)$_GET[\'visibility-filter\'];
}
$meta_query = array();
if ($queryParamsCounter > 1) {
$meta_query[\'relation\'] = \'AND\';
}
if (isset($cityId)) {
$meta_query[] = array(
\'key\' => \'city\',
\'value\' => $cityId,
\'compare\' => \'=\',
\'type\' => \'NUMERIC\',
);
}
if (isset($visibility)) {
$meta_query[] = array(
\'key\' => \'visibility\',
\'value\' => $visibility,
\'compare\' => \'=\',
\'type\' => \'NUMERIC\',
);
}
$query->set( \'meta_query\', $meta_query);
}
}
希望将来有人会觉得它有用。