首页 > 解决方案 > Loop 产品不包括在售仅显示 4 件商品

问题描述

我正在根据我找到的代码示例(仅检索到的原始产品)检索不包括在售产品的最新产品。这就是我所做的:

          <?php
            $args = array(
                'post_type' => 'product',
                'posts_per_page' => 8,
                'orderby' =>'id',
                'order' => 'DESC',
                'meta_query'     => array(
                    'relation' => 'OR',
                    array( // Simple products type
                        'key'           => '_sale_price',
                        'value'         => 0,
                        'compare'       => '=',
                        'type'          => 'numeric'
                    ),
                    array( // Variable products type
                        'key'           => '_min_variation_sale_price',
                        'value'         => 0,
                        'compare'       => '=',
                        'type'          => 'numeric'
                    )
                )
            );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
              <?php wc_get_template_part( 'content', 'product' ); ?>
        <?php endwhile; ?>
        <?php wp_reset_query(); ?>

实际上它只检索 4 种产品(我需要 8 种),这些不是最新的,但它正确地排除了特价产品。

任何想法?谢谢你。

标签: wordpresswoocommerce

解决方案


(已编辑)这将处理简单的产品。我试图找到如何处理变化,但找不到在创建销售价格时设置的任何元键。无论如何,“比较”不应该是“或”,而应该是“与”,因为您希望找到没有任何这些元值的产品。

$args = array(
                'post_type' => 'product',
                'posts_per_page' => 8,
                'orderby' =>'id',
                'order' => 'DESC',
                'meta_query'     => array(
                    array( 
                        'key'           => '_sale_price',
                        'compare'       => 'NOT EXISTS',

                    ),
                )
            );

推荐阅读