- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我花了一段时间试图让它发挥作用,并检查了一切看起来应该的东西。
我有一个页面,我在其中添加自定义排序函数,该函数会更改 WP Query使用pre_get_posts和set_query_var
简而言之,我有几个选择框,用户可以在其中选择一些内容来过滤循环。一旦用户提交查询,例如我的网址将如下所示:http://my-domain.com/gallery/?classroom=&digital_items=&kits_used=26155&project_type=&post_author=
因此,我添加了一篇帖子,其中包含 kits_used 的帖子元,并添加了一些“套件”,检查我的数据库中的 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;
}
如果我遗漏了一些东西,真的可以在这里使用一些帮助,一切看起来都在排队,但由于某种原因,除了“作者”排序下拉列表之外,它仍然无法工作。希望我提供了太多信息而不是不够:)
最佳答案
对于其他正在寻找类似内容的人,我找到了答案。由于元值作为数组存储在数据库中,因此我需要使用 LIKE 对这些特定查询进行比较。
这是完整的工作函数,请注意我在哪里使用 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;
}
关于php - 使用 pre_get_posts 构建自定义查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28918960/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!