33389

Woocommerce meta_query not working for featured products

I am trying to make a woocommerce slider carousel. but the meta_queryis not working only default slider is working. for example if i changed to featured=yes its not showing anything where i already set some featured product. Please help me to solve this issue. Woo-commerce Version is 3+.

<strong>My shortcode</strong>

[product_carousel title="LATEST PRODUCTS" per_page="6" featured="no" latest="yes" best_sellers="no" on_sale="no" orderby="menu_order" order="desc"]

and My function is

<?php function product_slider_carousel($atts){ global $wpdb, $woocommerce; $arg_s = shortcode_atts( array( 'title'=>'Product Slider', 'latest'=>'yes', 'per_page'=>'12', 'featured' => 'no', 'sale' => 'no', 'best_sellers'=>'no', 'on_sale'=>'no', 'orderby'=>'menu_order', 'order'=>'desc', ), $atts, 'product_carousel' ); //getting the values from shortcode $title = $arg_s['title']; $latest = $arg_s['latest']; $featured = $arg_s['featured']; $best_sellers = $arg_s['best_sellers']; $on_sale = $arg_s['on_sale']; $per_page = $arg_s['per_page']; $orderby = $arg_s['orderby']; $order = $arg_s['order']; $args = array( 'post_type' => array( 'product', 'product_variation' ), 'post_status' => 'publish', 'posts_per_page' => -1, 'ignore_sticky_posts' => 1, 'meta_query' => '', 'fields' => 'id=>parent' ); if(isset( $featured) && $featured == 'yes' ){ $args['meta_query'][] = array( 'key' => '_featured', 'value' => 'yes' ); } if(isset( $best_sellers) && $best_sellers == 'yes' ){ $args['meta_key'] = 'total_sales'; $args['orderby'] = 'meta_value'; $args['order'] = 'desc'; } if(isset( $on_sale) && $on_sale == 'yes' ){ $args['meta_key'] = '_sale_price'; $args['meta_compare'] = '>='; $args['meta_value'] = 0; $sale_products = get_posts( $args ); $product_ids = array_keys( $sale_products ); $parent_ids = array_values( $sale_products ); // Check for scheduled sales which have not started foreach ( $product_ids as $key => $id ) { if ( get_post_meta( $id, '_sale_price_dates_from', true ) > current_time('timestamp') ) { unset( $product_ids[ $key ] ); } } $product_ids_on_sale = array_unique( array_merge( $product_ids, $parent_ids ) ); set_transient( 'wc_products_onsale', $product_ids_on_sale ); } $query_args = array( 'posts_per_page'=> $per_page, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'order' => $order, 'meta_query' => $args['meta_query'], ); if(isset($atts['skus'])){ $skus = explode(',', $atts['skus']); $skus = array_map('trim', $skus); $query_args['meta_query'][] = array( 'key' => '_sku', 'value' => $skus, 'compare' => 'IN' ); } if(isset($atts['ids'])){ $ids = explode(',', $atts['ids']); $ids = array_map('trim', $ids); $query_args['post__in'] = $ids; } if ( isset( $category ) && $category!= 'null' && $category != 'a:0:{}' && $category != '0' && $category!="0, ") { $query_args['product_cat'] = $category; } if (strcmp($on_sale, 'yes') == 0 ) { if( empty( $product_ids_on_sale ) ) { return; } $query_args['post__in'] = $product_ids_on_sale; } if ( isset( $latest ) && $latest == 'yes' ) { $orderby = 'date'; $order = 'desc'; } switch( $orderby ) { case 'rand': $query_args['orderby'] = 'rand'; break; case 'date': $query_args['orderby'] = 'date'; break; case 'price' : $query_args['meta_key'] = '_price'; $query_args['orderby'] = 'meta_value_num'; break; case 'sales' : $query_args['meta_key'] = 'total_sales'; $query_args['orderby'] = 'meta_value_num'; break; case 'title' : $query_args['orderby'] = 'title'; break; } $the_query = new WP_Query( $query_args ); ob_start(); ?> <div class="row"> <div class="col-md-12"> <div class="product_wrap"> <div class="woocommerce"> <?php if (isset($title)&&$title!=''){ echo '<h4>'.$title.'</h4>'; }else{ echo '<h4>&nbsp;</h4>'; } ?> <ul class="products vpm-product-slider"> <?php if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); // Product Details get_template_part( "/templates/content", "product-shortcode" ); // Product Details endwhile; endif; ?> </ul> </div> </div> <!-- Query in Query--> </div> </div> <?php wp_reset_query(); return ob_get_clean(); } add_shortcode('product_carousel','product_slider_carousel'); ?>

Answer1:

In your code for featured products, you should use a <strong>tax query</strong> instead. You can see that easily in <strong>WC_Shortcode_Products</strong> source code for <strong>set_visibility_featured_query_args()</strong> private function.

if(isset( $featured) && $featured == 'yes' ){ $args['tax_query'][] = array( 'taxonomy' => 'product_visibility', 'terms' => 'featured', 'field' => 'name', 'operator' => 'IN', 'include_children' => false, // optional ); }

Since Woocommerce 3, <strong>"featured"</strong> product property is not anymore handled as postmeta data, but are now stored like a post term "featured" under <strong>'product_visibility'</strong> taxonomy, for better performances.

Answer2:

You're using the old way of querying products, WooCommerce 3 introduced a standard way of querying products. Here is an example of getting all the featured products. It's much easier to use and future proofs you code so you should rewrite what you have done and convert it to the new methods.

// Get all featured products. $args = array( 'featured' => true, ); $products = wc_get_products( $args );

Recommend

  • Wordpress - get 5 popular posts by views without plugin
  • Adding Product Programmatically in woocommerce
  • Eloquent Left Join, getting unexpected result
  • MySql update fields based on other fields
  • Woocommerce - List product by using specific attributes
  • SQL query to include specific WooCommerce tag
  • Encode string to match encoded form field name in PHP POST array
  • array_search() in Session Array
  • IE10 strips out hashtag from the URL
  • using html data-attributes as css-variable (i.e. text-shadow)
  • Understanding RTF and edit it with vb.net
  • allocating memory to an array of string
  • How to load gif image while ajax content is loading and javascript [duplicate]
  • Android distinguish between tap and double tap
  • netsh acl setting (need alternative method - registry settings?)
  • how do i write assembly code from c#?
  • How to get latest version of a artifact on Bintray using JSONP
  • Combining two different ActiveRecord collections into one
  • bad substitution shell- trying to use variable as name of array
  • wxPython: displaying multiple widgets in same frame
  • Stop Bash Script if Hive Fails
  • C: Incompatible pointer type initializing
  • Google Custom Search with transparent background
  • ilmerge with a PFX file
  • Why value captured by reference in lambda is broken? [duplicate]
  • Obtain ObjectIdHex value from mgo query
  • Volusion's generic SQL folder, functionality
  • How to set/get protobuf's extension field in Go?
  • JSON with duplicate key names losing information when parsed
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Upload files with Ajax and Jquery
  • Delete MySQLi record without showing the id in the URL
  • Rearranging Cells in UITableView Bug & Saving Changes
  • php design question - will a Helper help here?
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Why joiner is not used after Sequence generator or Update statergy
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Recursive/Hierarchical Query Using Postgres
  • UserPrincipal.Current returns apppool on IIS