php - WordPress 自定义查询在前端显示重复结果

标签 php mysql wordpress

我在 woocommerce 网站上进行了自定义搜索查询。 一切看起来都不错。但当我搜索 sku 编号之类的内容时,会显示两个搜索结果。

搜索结果示例
示例 -- http://bxcell.iteratemarketing.com/?s=BE0204&post_type=product

这是我的代码

add_filter('the_posts', 'woo_custom_fields_query');

    function woo_custom_fields_query($posts, $query = false) {
      if (is_search()){
        $noIds = array(0);
          foreach($posts as $post){
              $noIds[] = $post->ID;
          }

          $mPosts = get_post_by_custom_field(get_search_query(), $noIds);      

          if ($mPosts){
              foreach($mPosts as $product_id){
                  $posts[] = get_post($product_id->post_id);                
              }
          }
          return $posts;
      }
      return $posts;
    }

    function get_post_by_custom_field($qrval, $noIds) {
      global $wpdb, $wp_query;

      $pstArr = array();

      $noIdsQrl = implode(",", $noIds);
      $vrvspsts = $wpdb->get_results(
                      "
            SELECT p.post_parent as post_id FROM $wpdb->posts as p
            join $wpdb->postmeta pm
            on p.ID = pm.post_id         
            and pm.meta_value LIKE '%$qrval%'
            join $wpdb->postmeta visibility
            on p.post_parent = visibility.post_id    
            and visibility.meta_key = '_visibility'
            and visibility.meta_value <> 'hidden'
            where 1
            AND p.post_parent <> 0
            and p.ID not in ($noIdsQrl)
            and p.post_status = 'publish'
            group by p.post_parent
            "
      );

      foreach($vrvspsts as $post){
          $noIds[] = $post->post_id;
      }

      $noIdsQrl = implode(",", $noIds);

      $rglprds = $wpdb->get_results(
          "SELECT p.ID as post_id FROM $wpdb->posts as p
          join $wpdb->postmeta pm
          on p.ID = pm.post_id        
          AND pm.meta_value LIKE '%$qrval%' 
          join $wpdb->postmeta visibility
          on p.ID = visibility.post_id    
          and visibility.meta_key = '_visibility'
          and visibility.meta_value <> 'hidden'
          where 1
          and (p.post_parent = 0 or p.post_parent is null)
          and p.ID not in ($noIdsQrl)
          and p.post_status = 'publish'
          group by p.ID

    ");

      $pstArr = array_merge($vrvspsts, $rglprds);
      $wp_query->found_posts += sizeof($pstArr);    
      return $pstArr;
    }

提前致谢!

最佳答案

我认为,你的两个查询的逻辑有问题。您从两个结果的合并数组中获取重复的帖子 ID。

 $pstArr = array_unique(array_merge($vrvspsts, $rglprds));

关于php - WordPress 自定义查询在前端显示重复结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28079131/

相关文章:

php - 使用 Laravel 4 验证多个文件上传

java - 无法连接到数据库 [db] MySql Play Frameworks 2.5.10

mysql - 请求次数过多

php - Woocommerce 产品查询库存状态

javascript - 我想合并 DIV 中的所有图像并允许用户下载(js 驱动)

php - 引用用户上传的文件 - 数据库还是文件名?

php - 如何重定向到移动网站,同时仍然打开选项以查看桌面版本

php - SQLSTATE[42000] : Syntax error or access violation: 1055

mysql - 使用select创建表时锁定表

html - 单个 <br/> 不会创建换行符,而是需要两个