gpt4 book ai didi

php - 使用 pre_get_posts 构建自定义查询不起作用

转载 作者:行者123 更新时间:2023-11-29 22:50:42 26 4
gpt4 key购买 nike

我花了一段时间试图让它发挥作用,并检查了一切看起来应该的东西。

我有一个页面,我在其中添加自定义排序函数,该函数会更改 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com