2016-12-01 82 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