Problem: I have a taxonomy that includes terms for each day of the week. I would like to filter posts in a View using the current day of the week, so that only posts tagged with the term for today are shown.
Solution: Taxonomy term filters do not allow any date criteria in wp-admin, so custom code is required. Assuming the term slugs are "monday", "tuesday", and so on through "sunday", add the following code in your child theme's functions.php file to filter a View by term based on the local current day of the week:
add_filter( 'wpv_filter_query', 'only_todays_posts',99,3 ); function only_todays_posts( $query_args,$views_settings, $view_id) { if ($view_id == 12345){ $query_args = array( 'tax_query' => array( array( 'taxonomy' => 'day-of-the-week', 'field' => 'slug', 'terms' => strtolower(date('l')), 'operator' => 'IN' ), ), ); } return $query_args; }
Replace 12345 with the numeric ID of your View, and replace day-of-the-week with the slug of your custom taxonomy. Nothing extra is needed in wp-admin.
Relevant Documentation: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 2 replies, has 2 voices.
Last updated by 6 years, 10 months ago.
Assisted by: Christian Cox.