问题描述
我正在尝试弄清楚如何实现特定查询以修改我的 wordpress 帖子搜索结果。我正在尝试通过名为“共同作者”的自定义字段进行搜索。
可能有多个共同作者,这导致我的查询有时会失败。这是我现在所拥有的:
<?PHP
...
$query->set('Meta_key','common_authors');
$query->set('Meta_value',serialize( array(strval($_GET['common_author'])))); // I get a single ID from the url as a string
$query->set('Meta_compare','IN');
这是我在 var_dump 查询时看到的:
'Meta_key' => string 'common_authors'
'Meta_value' => string 'a:1:{i:0;s:5:"17145";}'
如果只有一个 common_author,这没问题。
但是,一个帖子可以有多个 common_authors。这是数据库中的元值示例:
a:4:{i:0;s:5:"14409";i:1;s:5:"17145";i:2;s:5:"14407";i:3;s:5:"14406";}
有人可以帮我弄清楚如何调整我的查询,以便它也返回这个查询吗?
解决方法
试试这个完美的工作!
$args = array(
'post_type' => 'post','meta_query' => array(
array(
'key' => 'common_authors','value' => array ( 'author1','author2','author3' ),'compare' => 'IN'
)
)
);
$query = new WP_QUERY($args);
如果您需要在 pre_get_posts 中使用它
$meta_query = array(
array(
'key' => 'common_authors','compare' => 'IN'
)
);
$query->set('meta_query',$meta_query);