2016-12-01 77 views
1

我有下面的代碼的結果:的WordPress:多個搜索字詞或連接來自多個WP_Queries

$args = array(
    's'     => 'Apple', 
    'cat'    => 80, 
    'posts_per_page' => -1, 
    'orderby'   => date, 
    'order'    => desc, 
); 
$query = new WP_Query($args); 
echo $query->found_posts.'<br /><br />'; 
if ($query->have_posts()) { 
    while ($query->have_posts()) { $query->the_post(); 
     echo the_time("Y.m.d"); echo ': '; echo the_title(); echo '<br />'; 
    } 
} 

它搜索80類別包含單詞蘋果任何職位,然後成功地輸出列表這樣的帖子像

2 

2016.10.10: Apple pie 
2016.10.01: Apple juice 

如果我也想顯示包含單詞香蕉的所有帖子,然後我重複剛纔的代碼's' => 'Apple'代之以's' => 'Banana'並獲得類似

1 

2016.10.05: Banana fresh 

很容易,直到我使用像

3 

2016.10.10: Apple pie 
2016.10.05: Banana fresh 
2016.10.01: Apple juice 

這裏的單一列表我需要你們的幫助,因爲我不知道如何實現這一希望輸出相關的搜索結果。

到目前爲止,我已經試過

$args = array(
    's'     => array('Apple','Banana'), 
    'cat'    => 80, 
    'posts_per_page' => -1, 
    'orderby'   => date, 
    'order'    => desc, 
); 

$args = array(
    array('s' => 'Apple',), 
    array('s' => 'Banana',), 
    'cat'    => 80, 
    'posts_per_page' => -1, 
    'orderby'   => date, 
    'order'    => desc, 
); 

兩個返回所有職位從給定的類別,即使只有一個子數組中的元素,因此它似乎's' couldn」放在一個子數組中或包含數組作爲參數。或者我錯了某處,但我無法捕捉到哪裏。

回答

1

當我在等待準備好的答案時,我也在研究PHP手冊和WP Codex,所以我最終以工作解決方案。

如果有人搜索答案同樣的問題,那就是:

<?php 

// Searching for posts with "Apple" in their content within category with the id 80 
$args_apple = array('s' => 'Apple', 'cat' => 80, 'posts_per_page' => -1); 
$query_apple = new WP_Query($args_apple); 
wp_reset_query(); 

// Searching for posts with "Banana" in their content within category with the id 80 
$args_banana = array('s' => 'Banana', 'cat' => 80, 'posts_per_page' => -1); 
$query_banana = new WP_Query($args_banana); 
wp_reset_query(); 

// Searching for posts with "Orange" in their content within category with the id 80 
$args_orange = array('s' => 'Orange', 'cat' => 80, 'posts_per_page' => -1); 
$query_orange = new WP_Query($args_orange); 
wp_reset_query(); 

// Joining results, excluding duplicates with wrapping array_merge() into array_unique() 
$result = new WP_Query(); 
$result->posts = array_unique(array_merge(
    $query_apple->posts, 
    $query_banana->posts, 
    $query_orange->posts 
), SORT_REGULAR); // While PHP manual states this parameter is 
        // optional for array_unique(), in this case 
        // it is obligatory 

// Showing the number of results 
$result->post_count = count($result->posts); 
echo $result->post_count.'<br /><br />'; 

// Sorting results by date 
usort($result->posts, 'order_by_date'); 
function order_by_date($a, $b) { 
    $a_date = $a->post_date; 
    $b_date = $b->post_date; 
    return (strcmp($a_date, $b_date) > 0) ? -1 : 1; // Sort descending 
// return (strcmp($a_date, $b_date) > 0) ? 1 : -1; // Sort ascending 
} 

// The search results output loop 
if ($result->have_posts()) { 
    while ($result->have_posts()) { $result->the_post(); 
     // Start making your own output 
     echo the_time("Y.m.d"); 
     echo ': '; 
     echo the_title(); 
     echo '<br />'; 
     // Stop making your own output 
    } 
} 
wp_reset_query(); 

?> 

我不跟所有的代碼唯一喜歡的上方是一個額外的功能合併數組排序。但是我花了大半天的時間來找到一種方法來對帶有內部參數的合併數組進行排序,但沒有成功。

的結果就像

7 

2016.10.21: Banana 
2016.10.15: Sliced orange 
2016.10.09: Banana fresh 
2016.10.07: Apple juice 
2016.10.05: Apple pie 
2016.10.03: Orange 
2016.10.01: Apple 

合併陣列的數量似乎是無限左右,只是不要忘了使用獨特$args_name$query_name每個WP_Query()

並始終關閉每個查詢與wp_reset_query()

1

儘管您的解決方案明顯起作用,但我想提交另一種可能的解決方案,以利用WordPress提供的過濾器。通過這種方式,我相信這個解決方案是「WordPress的路」更加一致:

// Hook into the WP_Query filter to modify the search query 
add_filter('posts_search', 'make_search_any_terms', 10, 2); 

/** 
* Function to convert search to "any" terms instead of "all" terms. 
* WordPress automatically passes in the two arguments. 
* 
* @param string $search 
* @param WP_Query $query 
* 
* @return string 
*/ 
function make_search_any_terms($search, $query) { 
    // If it's not a search, then do nothing 
    if (empty($query->is_search)) { 
     return $search; 
    } 

    global $wpdb; 

    $search_terms = get_query_var('search_terms'); 

    // This code adapted from WP_Query "parse_search" function 
    $search = ''; 
    $searchand = ''; 
    foreach ($search_terms as $term) { 
     $like      = '%' . $wpdb->esc_like($term) . '%'; 
     $q['search_orderby_title'][] = $wpdb->prepare("$wpdb->posts.post_title LIKE %s", $like); 

     $like = '%' . $wpdb->esc_like($term) . '%'; 
     $search .= $wpdb->prepare("{$searchand}(($wpdb->posts.post_title LIKE %s) OR ($wpdb->posts.post_content LIKE %s))", $like, $like); 
     // This is the significant change - originally is "AND", change to "OR" 
     $searchand = ' OR '; 
    } 

    if (! empty($search)) { 
     $search = " AND ({$search}) "; 
     if (! is_user_logged_in()) { 
      $search .= " AND ($wpdb->posts.post_password = '') "; 
     } 
    } 

    return $search; 
} 

與單個搜索查詢中使用它:

$args = array(
    's'     => 'Apple Banana Orange', 
    // ... 
); 

$results = WP_Query($args); 

你會得到有要麼全部結果蘋果香蕉橙色。

通過這種方式,您可以擁有一個查詢和一組結果,並簡單地遍歷它。

注意:上面的代碼通常放在您的主題的functions.php文件中。

+0

意外刪除我的評論,而不是編輯。 )無論如何,我只是想補充一點,你提供的功能已經促使我去解決如何僅通過發佈內容來限制搜索。非常感謝! )) – YKKY