自定义搜索WordPress的自定义帖子类型页面
问题描述:
我有一个相当简单的问题,我认为这将是相当普遍的,但经过几个小时的搜索和测试,我有点卡住了。自定义搜索WordPress的自定义帖子类型页面
我想要做的所有事情扩展了我的自定义帖子类型页面上默认“搜索帖子”功能的搜索能力。这里是我的代码,但是当我搜索一个已知的电子邮件地址时,它不会返回任何内容。我感觉我可能会完全吠叫错误的树。谁能推荐一个解决方案...
function iymp_modify_mp_posts_search($query) {
/*
* If admin and if my custom post type
*/
if (is_admin() && $query->query_vars['post_type'] === 'mp_post') {
/*
* Show 200 posts per page in ascending order
*/
$query->set('posts_per_page', '200');
$query->set('order', 'ASC');
/*
* If user entered a search term
*/
if (isset($_GET['s'])) {
/*
* As well as searching in the title (default behaviour), search in
* the _email_key field as well.
* *** Doesn't work ***
*/
$query->query_vars['meta_query'][] = array(
'key' => '_email_key',
'value' => $_GET['s'],
'compare' => 'LIKE',
);
}
}
}
add_action('pre_get_posts', 'iymp_modify_mp_posts_search');
也许问题是,WP是取与在标题和_email_key领域的搜索。如果这是问题,我会如何处理它们?
答
问题的确在于WP在标题和_email_key字段中进行了AND搜索。
要做OR搜索,我尝试合并WP_Query结果,如下所示 - https://wordpress.stackexchange.com/questions/55519/can-i-merge-2-new-wp-queryvariable-s - 在guidod的答案中。尽管如此,这并不是一个好的解决方案,并导致了不稳定的行为。
正确的解决方案,我发现是修改使用WP自定义查询查询如图所示的代码(这需要一些修改)在这里 - http://codex.wordpress.org/Custom_Queries ..