3

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

第二个查询设置为使用“SKU”的 postmeta 键搜索任何帖子,例如搜索查询。

我正在尝试组合这两个查询,以便搜索将返回标题或 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 与搜索词匹配的任何帖子?

4

1 回答 1

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 );
于 2013-11-05T13:07:20.947 回答