最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - Show list of woo commerce orders where particular zip code exists using pre_get_posts?

programmeradmin3浏览0评论

I am working on a project where I have to show a list of woo-commerce orders where a particular postcode/zip code exists. I am using hook pre_get_posts to achieve that when I pass a single key and value order start showing but when I add multiple key and values no order is showing. The code I am using is from wordpress.stackexchange but still, it is not working for multiple keys and values.

I am sure there is something wrong in the code that I can identify any help will be appreciated. Thanks

add_action( 'pre_get_posts', array($this, 'zipcode_wise_post_filter') );
function zipcode_wise_post_filter( $query ) {

if( !$query->is_main_query() ) return;

if(isset(wp_get_current_user()->roles[0]) && wp_get_current_user()->roles[0] == 'order_manager'){
    $user_id = get_current_user_id();
    $zip_code = array(60643,1600, 02134);


    $meta_query = $query->get("meta_query");
    $meta_query = array(
        "relation" => "AND",
    );

    $query->set('post_type', 'shop_order' );

    foreach ($zip_code as $code) {
      $meta_query[]  =   array(
          'key'=>'_billing_postcode',
          'value'=>$code,
          'compare' => 'EXISTS',
      );
    }


    $query->set('meta_query',$meta_query ); 

} 

return $query; 
}

发布评论

评论列表(0)

  1. 暂无评论