在WooCommerce中按标签选择产品的SQL查询?

时间:2013-11-17 11:35:20

标签: mysql sql wordpress woocommerce

我有一个WooCommerce商店,其商品标记为智能手机或平板电脑。我正在寻找一种从数据库中选择所有智能手机的方法,所以有人知道原始SQL查询要按标签选择所有WooCommerce产品吗?

1 个答案:

答案 0 :(得分:0)

这将为您提供使用本机WordPress查询所需的结果。然后,您可以使用循环遍历结果:

// Define Query Arguments
$args = array(
  'post_type' => 'product',
  'product_tag' => $tags
);

// Create the new query
$loop = new WP_Query( $args );
// Get products number
$product_count = $loop->post_count;

// If results
if( $product_count > 0 ) :
  // Start the loop
  while ( $loop->have_posts() ) : $loop->the_post(); global $product;

   /** Do stuff here **/

  endwhile;

else :
  _e('No product matching your criteria.');
endif; // endif $product_count > 0

此解决方案可获得Remi Corson