【问题标题】:Limit Woocommerce featured products in a WP_Query在 WP_Query 中限制 Woocommerce 特色产品
【发布时间】:2018-07-31 02:15:51
【问题描述】:

我想在网站的标题中获得 3 个特色产品。但我的查询不断返回无限数量的结果。

我一直在网上寻找解决方案,并遇到了所有答案都在查询方面说相同内容的答案。我可能做错了什么?

$meta_query  = WC()->query->get_meta_query();
$tax_query   = WC()->query->get_tax_query();
$tax_query[] = array(
    'taxonomy' => 'product_visibility',
    'field'    => 'name',
    'terms'    => 'featured',
    'operator' => 'IN',
);

$args = array(
    'post_type'           => 'product',
    'post_status'         => 'publish',
    'posts_per_page'      => 2,
    'meta_query'          => $meta_query,
    'tax_query'           => $tax_query,
);

$featured_query = new WP_Query( $args );

if ($featured_query->have_posts()) {

    while ($featured_query->have_posts()) : 

        $featured_query->the_post();

        $product = get_product( $featured_query->post->ID );

        echo $product->title; echo "test";
        // Product info here

    endwhile;

}

wp_reset_query();

以下查询返回 20 个结果。代码放在 header.php 中。使用 woocommerce 3.x。

【问题讨论】:

    标签: php wordpress woocommerce product custom-taxonomy


    【解决方案1】:

    首先你的代码有点过时了,因为 Woocommerce 3,get_product() 需要替换为 wc_get_product()$product->title; $product->get_title();...
    完成代码后,您将获得 3 款特色产品:

    $meta_query  = WC()->query->get_meta_query();
    $tax_query   = WC()->query->get_tax_query();
    $tax_query[] = array(
        'taxonomy' => 'product_visibility',
        'field'    => 'name',
        'terms'    => 'featured',
        'operator' => 'IN',
    );
    
    $featured = new WP_Query( array(
        'post_type'           => 'product',
        'post_status'         => 'publish',
        'posts_per_page'      => 3, // <==  <==  <==  3 products
        'meta_query'          => $meta_query,
        'tax_query'           => $tax_query,
    ) );
    
    // Get the products count in the query
    echo '<p>Featured products count: ' .$featured->post_count . '</p>';
    
    if ($featured->have_posts()) : while ($featured->have_posts()) : 
        $featured->the_post();
    
        $product = wc_get_product( $featured->post->ID );
    
        echo $product->get_title() . '<br>';
        // Product info here
    
    endwhile; endif;
    
    wp_reset_postdata();
    

    它应该对你有用,因为我已经在 header.php 文件中成功测试了这段代码……

    与 Woocommerce 3 之前一样,“特色产品”由发布元数据(元查询)处理,您可能需要在 Woocommerce 设置 > 状态 > 工具中更新产品术语计数。在“Term counts”部分点击“Recount terms”。

    【讨论】:

      【解决方案2】:

      您应该使用wp_reset_postdata() 而不是wp_reset_query(),因为WP_query 不会覆盖主查询。

      如果这不能解决您的问题,请确保任何其他自定义循环使用适当的重置,和/或尝试重命名变量 $featured_query 如果您在其他地方使用它 - 它可能是从前一个循环继承帖子.

      您也可以尝试添加 'nopaging' =&gt; true'ignore_sticky_posts' =&gt; true 参数

      我不想建议,但如果你不明白为什么它返回 20 个帖子而不是 2 个,你可以在 break 你的 while 循环使用计数器:

      if ($featured_query->have_posts()) {
          $counter = 0;
          while ($featured_query->have_posts()) : $featured_query->the_post();
      
              /* Do post stuff here */
      
              $counter++;
      
              if( $counter == 2 ) break;
          endwhile;
      }
      

      【讨论】:

        猜你喜欢
        • 2018-02-25
        • 2016-09-08
        • 1970-01-01
        • 1970-01-01
        • 2018-08-26
        • 2013-02-22
        • 1970-01-01
        • 2018-05-17
        • 1970-01-01
        相关资源
        最近更新 更多