我一直在努力让这个工作起来并查看它应该的一切。
我有一个页面,我在其中添加了一个自定义排序函数,该函数使用WP Query和set_query_var
来更改pre_get_posts简而言之,我有几个选择框,用户可以选择一些东西来过滤循环。用户提交查询后,例如我的网址将如下所示:http://my-domain.com/gallery/?classroom=&digital_items=&kits_used=26155&project_type=&post_author=
所以我添加了一篇文章,其中包含了kits_used的post meta,并添加了一些"工具包",检查我的数据库中的postmeta,并看到meta_value具有类似于它的数组:a:2:{i:0;s:5:"26155";i:1;s:5:"26152";}
但是,我觉得我的pre_get_posts函数有问题,因为当我选择ID为26155的工具包时,它说没有找到任何帖子。不知道我是否正确地做了它,因为它是一个数组或者如果我在这里遗漏了其他东西。
我的功能:
add_action('pre_get_posts', 'gallery_sort_query');
function gallery_sort_query( $query )
{
// validate
if( is_admin() )
{
return;
}
if( !$query->is_main_query() )
{
return;
}
// get original meta query
$meta_query = $query->get('meta_query');
// allow the url to alter the query
if( !empty($_GET['kits_used']) )
{
$kits_used = explode(',', $_GET['kits_used']);
//Add our meta query to the original meta queries
$meta_query[] = array(
'key' => 'kits_used',
'value' => $kits_used,
'compare' => 'IN',
);
}
if( !empty($_GET['project_type']) )
{
$project_type = explode(',', $_GET['project_type']);
//Add our meta query to the original meta queries
$meta_query[] = array(
'key' => 'project_type',
'value' => $project_type,
'compare' => 'IN',
);
}
if( !empty($_GET['digital_items']) )
{
$digital_items = explode(',', $_GET['digital_items']);
//Add our meta query to the original meta queries
$meta_query[] = array(
'key' => 'digital_items',
'value' => $digital_items,
'compare' => 'IN',
);
}
if( !empty($_GET['classroom']) )
{
$classroom = explode(',', $_GET['classroom']);
//Add our meta query to the original meta queries
$meta_query[] = array(
'key' => 'classroom',
'value' => $classroom,
'compare' => 'IN',
);
}
if( !empty($_GET['post_author']) )
{
$author = $_GET['post_author'];
$query->set( 'author' , $author );
}
// update the meta query args
$query->set('meta_query', $meta_query);
// always return
return;
}
真的可以在这里使用一些帮助,以防我遗漏了什么,一切看起来都在排队但是由于某种原因它仍然无法正常工作,除了"作者"排序下拉。希望我提供的信息太多而不够:)
答案 0 :(得分:0)
对于其他寻找类似内容的人,我找到了答案。因为元值作为数组存储在数据库中,所以我需要使用LIKE来对这些特定查询进行比较。
这是完整的工作函数,请注意我对于作为数组的meta_values使用LIKE而不是IN。
add_action('pre_get_posts', 'gallery_sort_query');
function gallery_sort_query( $query )
{
// validate
if( is_admin() )
{
return;
}
if( !$query->is_main_query() )
{
return;
}
// get original meta query
$meta_query = $query->get('meta_query');
// allow the url to alter the query
if( !empty($_GET['kits_used']) )
{
$kits_used = explode(',', $_GET['kits_used']);
//Add our meta query to the original meta queries
$meta_query[] = array(
'key' => 'kits_used',
'value' => $kits_used,
'compare' => 'LIKE',
);
}
if( !empty($_GET['project_type']) )
{
$project_type = explode(',', $_GET['project_type']);
//Add our meta query to the original meta queries
$meta_query[] = array(
'key' => 'project_type',
'value' => $project_type,
'compare' => 'IN',
);
}
if( !empty($_GET['digital_items']) )
{
$digital_items = explode(',', $_GET['digital_items']);
//Add our meta query to the original meta queries
$meta_query[] = array(
'key' => 'digital_items',
'value' => $digital_items,
'compare' => 'LIKE',
);
}
if( !empty($_GET['classroom']) )
{
$classroom = explode(',', $_GET['classroom']);
//Add our meta query to the original meta queries
$meta_query[] = array(
'key' => 'classroom',
'value' => $classroom,
'compare' => 'IN',
);
}
if( !empty($_GET['post_author']) )
{
$author = $_GET['post_author'];
$query->set( 'author' , $author );
}
// update the meta query args
$query->set('meta_query', $meta_query);
// always return
return;
}