Php Woocommerce-Guys I';我试图显示3个随机产品,但跳过了前3个最近添加的产品

Php Woocommerce-Guys I';我试图显示3个随机产品,但跳过了前3个最近添加的产品,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我需要你的帮助。我试图显示3个随机产品,但跳过了前3个最近添加的产品。最新的含义不是按查询,而是按产品创建的全局日期 这是我用来显示随机产品的代码 $args = array( 'post_type' => 'product', 'orderby' => 'rand', 'posts_per_page' => 3, ); $loop = new WP_Query( $args ); if ( $loop->have_posts(

我需要你的帮助。我试图显示3个随机产品,但跳过了前3个最近添加的产品。最新的含义不是按查询,而是按产品创建的全局日期

这是我用来显示随机产品的代码

$args = array(
'post_type'         => 'product',
'orderby'           => 'rand',
'posts_per_page'    => 3,
);

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
}

添加“偏移量”仅跳过前3个随机变量。有没有办法跳过前三个最近添加的产品?

首先,使用
wp\u get\u recent\u posts
函数和映射ID获取最后三个产品的ID,然后使用这三个post ID将
post\u not\u in
参数添加到
wp\u query

$recent_posts = wp_get_recent_posts([
    'post_type'   => 'product',
    'numberposts' => 3
]);

$last_three_posts = array_map(function($a) { return $a['ID']; }, $recent_posts);

$args = array(
    'post_type'         => 'product',
    'orderby'           => 'rand',
    'posts_per_page'    => 3,
    'post__not_in'      => $last_three_posts,
);

$loop = new WP_Query( $args );

请指定最近一次随机查询或全局日期,以便最后3次添加的产品不会显示在结果中?@Artem谢谢。最近一次是在产品创建的全球日期。先生。你为什么这么棒?我是个新手,我永远也解决不了这个问题。非常感谢。@jmcabran没问题,我们都是初学者)