2013-12-23 84 views
0

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

回答

0

問題的確在於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 ..

相關問題