如何组合这两个Wordpress搜索查询?

时间:2013-11-04 19:19:56

标签: php mysql wordpress woocommerce

我有两个搜索查询。对于与参数匹配的任何帖子标题,可以使用默认方式进行搜索。

第二个查询设置为搜索任何帖子,其中的帖子键为“SKU”,如搜索查询。

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

首先查询:

$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的所有帖子?

1 个答案:

答案 0 :(得分:0)

我假设你将在循环中使用args。

您可以使用循环将所有返回的post_id添加到数组中。您可以运行两个单独的循环,并将所有条目添加到数组中。您需要检查双重条目,因此您最终不会两次打印相同的帖子。

所以你会做类似的事情 -

//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 );