WordPress:多个搜索词或连接多个 WP_Queries 的结果

YKK*_*KKY 1 php wordpress

我有以下代码:

$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 />';
    }
}
Run Code Online (Sandbox Code Playgroud)

它在类别 80 中搜索包含“Apple”一词的任何帖子,然后成功输出此类帖子的列表,例如

2

2016.10.10: Apple pie
2016.10.01: Apple juice
Run Code Online (Sandbox Code Playgroud)

如果我还想显示包含“Banana”一词的所有帖子,那么我只需重复代码并's' => 'Apple'替换为's' => 'Banana'并得到类似的内容

1

2016.10.05: Banana fresh
Run Code Online (Sandbox Code Playgroud)

非常简单,直到我想使用单个列表输出两个搜索结果,例如

3

2016.10.10: Apple pie
2016.10.05: Banana fresh
2016.10.01: Apple juice
Run Code Online (Sandbox Code Playgroud)

在这里我需要你的帮助,因为我不知道如何实现这一点。

到目前为止我已经尝试过

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

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

即使子数组中只有一个元素,两者都会返回给定类别中的所有帖子,因此似乎's'无法将其放入子数组中或包含数组作为参数。或者我在某个地方打错了,但我不知道在哪里。

ran*_*ame 5

虽然您的解决方案显然有效,但我想提交另一个可能的解决方案,该解决方案利用 WordPress 提供的过滤器。这样看来,我相信这个解决方案更符合“The WordPress Way”:

// 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;
}
Run Code Online (Sandbox Code Playgroud)

要将其与单个搜索查询一起使用:

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

$results = WP_Query( $args );
Run Code Online (Sandbox Code Playgroud)

您将得到包含苹果香蕉橙子的所有结果。

通过这种方式,您可以拥有单个查询和一组结果,并简单地循环遍历它。

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