Mysql
 sql >> Base de données >  >> RDS >> Mysql

Comment combiner ces deux requêtes de recherche Wordpress ?

Je suppose que vous utiliserez des arguments dans la boucle.

Vous pouvez utiliser la boucle pour ajouter tous les post_ids retournés à un tableau. Vous pouvez exécuter deux boucles distinctes et ajouter toutes les entrées à un tableau. Vous auriez besoin de vérifier les doubles entrées, afin de ne pas imprimer deux fois le même message.

Alors vous feriez quelque chose comme-

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