简体   繁体   中英

How do I combine these two Wordpress search queries?

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

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

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

First query:

$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'
                )
            )
        );

Second query:

        $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'
        )
        )
    );

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM