如何结合这两个Wordpress搜索查询? [英] How do I combine these two Wordpress search queries?

查看:106
本文介绍了如何结合这两个Wordpress搜索查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个搜索查询.一个以默认方式搜索与参数匹配的帖子标题.

I have two search queries. One searches in the default manner, for any post titles that match the arguments.

第二个查询设置为使用与搜索查询类似的后置键"SKU"搜索任何帖子.

The second query is set to search any posts with the postmeta key of "SKU" LIKE the search query.

我正在尝试将这两个查询组合在一起,以便搜索将返回标题或sku与搜索词匹配的所有帖子.

I am trying to combine these two queries so that the search will return any posts whose title OR sku match the search term.

第一个查询:

$args = array(
            's'                     => apply_filters('yith_wcas_ajax_search_products_search_query', $search_keyword),
            'post_type'             => 'product',
            'post_status'           => 'publish',
            'ignore_sticky_posts'   => 1,
            'orderby'               => $ordering_args['orderby'],
            'order'                 => $ordering_args['order'],
            'posts_per_page'        => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')),
            'meta_query'            => array(
                array(
                    'key'           => '_visibility',
                    'value'         => array('catalog', 'visible'),
                    'compare'       => 'IN'
                )
            )
        );

第二个查询:

        $args = array(
        'post_type'             => 'product',
        'post_status'           => 'publish',
        'ignore_sticky_posts'   => 1,
        'orderby'               => $ordering_args['orderby'],
        'order'                 => $ordering_args['order'],
        'posts_per_page'        => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')),
        'meta_query'            => array(
            array(
                'key'           => '_visibility',
                'value'         => array('catalog', 'visible'),
                'compare'       => 'IN'
            ),
        array(
            'key'           => '_sku',
            'value'         => apply_filters('yith_wcas_ajax_search_products_search_query', $search_keyword),
            'compare'       => 'LIKE'
        )
        )
    );

如何结合这两个查询,并返回标题或与搜索字词匹配的sku的任何帖子?

How can I combine these two queries, and return any posts with the title or the sku matching the search term?

推荐答案

我假设您将在循环中使用args.

I'm assuming you will be using args in the loop.

您可以使用循环将所有返回的post_id添加到数组中.您可以运行两个单独的循环,然后将所有条目添加到数组中.您将需要检查两次输入,这样您就不会最终两次打印同一篇文章.

You can use the loop to add all the returned post_ids to an array. You could run two seperate loops, and add all the entries to an array. You would need to check for double entries, so you don't end up printing the same post twice.

所以你会做类似的事情-

So you would do something like-

//First loop    
$args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'ignore_sticky_posts'   => 1,
    'orderby'               => $ordering_args['orderby'],
    'order'                 => $ordering_args['order'],
    'posts_per_page'        => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')),
    'meta_query'            => array(
        array(
            'key'           => '_visibility',
            'value'         => array('catalog', 'visible'),
            'compare'       => 'IN'
        ),
    )
);
while ( $loop->have_posts() ) : $loop->the_post();
    $post_id = get_the_ID();
    $my_post = my_post_function($post_id);
    //Store the items in an array
    $my_post_array [] = $my_post;
    query_posts($args); 
    endwhile;

 //Second loop 
  $args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'ignore_sticky_posts'   => 1,
    'orderby'               => $ordering_args['orderby'],
    'order'                 => $ordering_args['order'],
    'posts_per_page'        => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')),
    'meta_query'            => array(
     array(
        'key'           => '_sku',
        'value'         => apply_filters('yith_wcas_ajax_search_products_search_query', $search_keyword),
        'compare'       => 'LIKE'
    )
    )
);
while ( $loop->have_posts() ) : $loop->the_post();
    $post_id = get_the_ID();
    $my_post = my_post_function($post_id);
    //Store the items in an array
    $my_post_array [] = $my_post;
    query_posts($args); 
    endwhile;

//Remove duplicate entries from the array
array_unique ( $my_post_array, SORT_STRING );

这篇关于如何结合这两个Wordpress搜索查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆