Skip Navigation

[Resolved] How to filter a date field by an specific day of the week

This thread is resolved. Here is a description of the problem and solution.

Problem:

Filter the datetime field (for example i have a custom post type with a starting date) with an specific day of the week. I want to set a view to show events on monday, tuesday, wednesday, thursday and friday.

Solution:

There isn't such a built-in feature within Views plugin, But it is possible with some custom codes, for example:

https://toolset.com/forums/topic/how-to-filter-a-date-field-by-an-specific-day-of-the-week/#post-1124389

Relevant Documentation:

This support ticket is created 6 years, 2 months 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 2 replies, has 2 voices.

Last updated by Martín Mellado 6 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#1124074

I am trying to:
Filter the datetime field (for example i have a custom post type with a starting date) with an specific day of the week. I want to set a view to show events on monday, tuesday, wednesday, thursday and friday. The date filter only supports

I expected to see:
The same view or multiple views, but each one only showing events happening on specific days of the week.

Thank you

#1124389

Hello,

There isn't such a built-in feature within Views plugin, see our document:
https://toolset.com/documentation/user-guides/date-filters/

No such an option to filter the posts with an specific day of the week.

But it is possible with some custom codes, for example, you can try these:
1) Add below PHP codes into your theme/functions.php


add_shortcode('ts_day_week', function($atts){
	$atts = shortcode_atts( array(
		'day' => 'Monday',
	), $atts);
	$res = strtotime('last ' . $atts['day']);
	if(date('l') == $atts['day']){
		$res = strtotime('this ' . $atts['day']);
	}
	return $res;
});

Use it to retrieve the timestamp value of the specific day of current week.

2) Dashboard-> Toolset-> Settings-> Front-end Content
in section "Third-party shortcode arguments", add above shortcode name: ts_day_week

3) Create a post view, query posts of your custom post type, filter by:
Select items with field:
starting date is a number equal to VIEW_PARAM(ts)

4) Then display the view's shortcode like this:

This Monday: 
[wpv-view name="VIEWS-NAME" ts='[ts_day_week day="Monday"]']

This Tuesday: 
[wpv-view name="VIEWS-NAME" ts="[ts_day_week day="Tuesday"]"]

...

And test again.

#1128244

Thank you for the answer, it explains more than i was expecting and i think is better in this case, so thank you! i will try other combinations too.