dongwen5019 2013-11-04 19:19
浏览 13
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • doujiexin1136 2013-11-05 13:07
    关注

    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 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算