我有一個相當簡單的問題,我認爲這將是相當普遍的,但經過幾個小時的搜索和測試,我有點卡住了。自定義搜索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領域的搜索。如果這是問題,我會如何處理它們?