wordpress - 在 Wordpress Woocommerce 中显示上个月添加的产品的最简单方法是什么?
问题描述
我想创建一个仅显示上个月发布的产品的 Woocommerce 商店页面。实现这一目标的最佳方法是什么?我试过几个插件都没有成功。据我了解,您可以使用标准的 Woocommerce、Woocommerce 块和一些短代码来完成此操作。
任何建议表示赞赏。
解决方案
尝试这个。这两个答案的组合(归功于他们。我只是根据您的要求更新代码)。将此代码放入您的活动主题 functions.php 文件中。
https://stackoverflow.com/a/66528155/6469645
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;
}
推荐阅读
- mysql - 从同一张表中查询两个具有不同关系的键
- android - 如何在没有完成的情况下刷新页面?
- javascript - 按名称对数组数据进行分组
- ruby-on-rails - 通过传递参数数组上传多个文件而没有任何宝石
- sql - 避免 SQL 表中重复的“对”值(两列)
- azure-resource-manager - 未读取 Azure ARM 参数文件中的值
- javascript - 如何将两个json与不同的对象合并但不合并现有对象
- reactjs - React useContext,useRedux子组件不更新
- c# - c# 使用newtonsofts JsonConvert DeserializeObject的通用数据对象
- java - 如何使用 java 和 mysql 为员工代码生成唯一的自定义序列 ID