首页 > 解决方案 > 在 Wordpress Woocommerce 中显示上个月添加的产品的最简单方法是什么?

问题描述

我想创建一个仅显示上个月发布的产品的 Woocommerce 商店页面。实现这一目标的最佳方法是什么?我试过几个插件都没有成功。据我了解,您可以使用标准的 Woocommerce、Woocommerce 块和一些短代码来完成此操作。

任何建议表示赞赏。

标签: wordpresswoocommerceshortcode

解决方案


尝试这个。这两个答案的组合(归功于他们。我只是根据您的要求更新代码)。将此代码放入您的活动主题 functions.php 文件中。

https://stackoverflow.com/a/66528155/6469645

Woocommerce:仅显示开始日期和结束日期之间的产品

function custom_meta_query( $meta_query ){

    $start_date = array(
        'year'  => date("Y", strtotime("first day of previous month")),
        'month' => date("n", strtotime("first day of previous month")),
        'day'   => date("j", strtotime("first day of previous month"))
    );

    $end_date = array(
        'year'  => date("Y", strtotime("last day of previous month")),
        'month' => date("n", strtotime("last day of previous month")),
        'day'   => date("j", strtotime("last day of previous month"))
    );

    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'date_query' => array(
            array(
                'after'     => $start_date,
                'before'    => $end_date,
                'inclusive' => true,
            ),
        ),
        'posts_per_page' => -1,
    );

    return $args;
}

// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
    if( ! is_admin() )
        return custom_meta_query( $meta_query );
}

// The shortcode products query
add_filter( 'woocommerce_shortcode_products_query', 'custom__shortcode_products_query', 10, 3 );
function custom__shortcode_products_query( $query_args, $atts, $loop_name ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}

// The widget products query
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}

推荐阅读