我正在尝试根据repeater字段的ACF子字段值组织搜索结果页面。我想要一个相似的结果,而不是=。
到目前为止,我获得了以下信息(在下面的帮助下进行了修改,但仍然不起作用):
// Modify meta key to allow wildcard
function add_wildcard_to_meta_key_filter( $where ) {
$where = str_replace("meta_key = \'test_repeater_%", "meta_key LIKE \'test_repeater_%", $where);
return $where;
}
add_filter(\'posts_where\', \'add_wildcard_to_meta_key_filter\');
//Modify search query
function alter_search_query($query) {
if ( !$query->is_search )
return $query;
$search = $query->query;
$query->set(\'post_type\' ,\'page\');
$query->set(\'meta_query\', array(
array(
\'meta_key\' => \'test_repeater_%_test_sub_field\',
\'meta_value\' => \'%\'.$search.\'%\',
\'compare\' => \'LIKE\',
),
));
$query->set(\'s\', \'\');
}
add_action(\'pre_get_posts\',\'alter_search_query\');
如果我加上确切的值,即“beef tenderloin”,效果很好,但如果我输入“beef”,则失败。如何使搜索更加通用?
我安装了查询监视器插件,并注意到我得到了以下SQL
SELECT SQL_CALC_FOUND_ROWS wpfb_posts.ID
FROM wpfb_posts
INNER JOIN wpfb_postmeta
ON ( wpfb_posts.ID = wpfb_postmeta.post_id )
WHERE 1=1
AND ( ( wpfb_postmeta.meta_key LIKE \'test_repeater_%_test_sub_field\'
AND wpfb_postmeta.meta_value IN (\'beef\') ) )
AND wpfb_posts.post_type = \'page\'
AND (wpfb_posts.post_status = \'publish\'
OR wpfb_posts.post_status = \'acf-disabled\'
OR wpfb_posts.post_status = \'private\')
GROUP BY wpfb_posts.ID
ORDER BY wpfb_posts.post_date DESC
LIMIT 0, 10
如果我更改此选项:
AND ( ( wpfb_postmeta.meta_key LIKE \'test_repeater_%_test_sub_field\'
AND wpfb_postmeta.meta_value IN (\'beef\') ) )
对此:
AND ( ( wpfb_postmeta.meta_key LIKE \'test_repeater_%_test_sub_field\'
AND wpfb_postmeta.meta_value LIKE \'%beef%\' ) )
然后在phpMyAdmin的SQL中运行它,我得到了所需的行。问题是,如何使用WordPress函数更改查询的外观?