I'm creating a flash sale website and I already display products according to date range on my home and shop pages. But I also want to display products according to a date range elsewhere and therefore using a shortcode.
Here is my code:
function testt($meta_query)
{
$today = current_time('Ymd');
$args = apply_filters('woocommerce_shortcode_products_query', array (
'post_type' => 'product',
'numberposts' => -1,
'meta_query' => array(
'relation' => 'AND',
'start_clause' => array(
'key'=>'flash_sale_start',
'value' => $today,
'compare'=> '<=',
'type' => 'DATE'
),
'end_clause' => array(
'key' => 'flash_sale_end',
'value' => $today,
'compare' => '>=',
'type' => 'DATE'
),
)));
return $args;
}
add_shortcode( 'test', 'testt' );
But it doesn't display anything, even the rest of my page content has disappeared.
What I am doing wrong?
Any help is appreciated.
This is normal that it doesn't return anything as you need to pass this
$args
in aWP_Query
first and to call a the product template in a loop this way:Code goes in function.php file of the active child theme (or active theme).
USAGE:
There is 4 available optional arguments that you can add to this shortcode:
columns
(The number of columns) - Default is 4limit
(the number of products | -1 will display all ) - Default is 20start
(Start date | format is 'YMD' ) - Default is todayend
(End date | format is 'YMD' ) - Default is todayYou can set any more in the function… You can change default values too.
Example 1 (simple with default values):
Example 2 (some custom values)
Tested and works