Skip Navigation

[Resolved] Filter view to only display posts published yesterday

This support ticket is created 3 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by Rita 3 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#2193209

Hi there

I have a simple view of woocommerce orders, ordered by post date ascending, with a few filters to specify status etc.

I also need to filter the view to only show orders that were created/published yesterday.

After reading several support tickets on the subject, rather than using the view's date filter which has discrepancies when the year changes, I have tried the following in functions.php but... whatever I enter makes no difference to the output. The view only displays orders created today.

Here are the two I have tried so far that don't work. Note that on the first I tried with just one view and on the second I tried with all the views I need to use this filter with. If I change any of the elements nothing happens. The view continues to display today's orders.

add_filter( 'wpv_filter_query', 'get_past_one_month_posts', 10, 2);
function get_past_one_month_posts( $query_args ,$view_settings ) {
if (isset($view_settings['view_id']) && $view_settings['view_id'] == 33534) {
$query_args['date_query'] = array(
'column' => 'post_date',
'after' => '1 day ago',
);
}
return $query_args;
}

-----
OR
-----

add_filter( 'wpv_filter_query', 'limit_query_one_day', 10, 3 );
function limit_query_one_day($query_args, $view_setting, $view_id){
if (in_array($view_id, array( 33534, 33545, 33537, 33551, 33538, 33552 ))) {
$query_args['date_query'] = array(
array(
'before' => '0 day ago',
'after' => '1 day ago'
)
);
}
return $query_args;
}

Hoping someone can easily spot what the problem is...

Thanks in advance!

Rita

#2193233

Oops I think I have got it... yes?

add_filter( 'wpv_filter_query', 'get_past_one_month_posts', 10, 2);
function get_past_one_month_posts( $query_args ,$view_settings ) {
if (isset($view_settings['view_id']) && $view_settings['view_id'] == 33534) {
$query_args['date_query'] = array(
'column' => 'post_date',
'before' => '1 day ago',
'after' => '2 day ago',
);
}
return $query_args;
}

#2193597

Hello,

Thanks for sharing the solution, it will help other users.

And here is the document about data parameter of WP_Query, for your reference:
https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

#2196501

Hi Luo

Totally welcome. This forum has helped me so many times I can't remember. I am very happy to contribute in any way I can.

Just to note that the above filter doesn't actually get "yesterday's" posts. It gets the day before yesterday. So to get "yesterday's" posts I changed it to:

'column' => 'post_date',
'before' => '0 day ago',
'after' => '1 day ago',

Then I also wanted to filter many views to get posts published "yesterday". Here is what is working for me. Just replace the view id's - 12345, 12346, 12347, 12348 - with your own view id's:

add_filter('wpv_filter_query', 'get_yesterday_posts', 99, 3);
function get_yesterday_posts($view_args, $view_settings, $view_id){
$view_ids = array( 12345, 12346, 12347, 12348 );
if ( in_array($view_id, $view_ids) ) {
$view_args['date_query'] = array(
'column' => 'post_date',
'before' => '0 day ago',
'after' => '1 day ago',
);
}
return $view_args;
}

Hope this is useful.

Rita

#2196503

Closing the thread. Thank you!