Hi,
I'm creating a View, and I'd like to filter by day of week. In the CPT, my custom taxonomy stores data as day of week in string format, e.g. Monday, Tuesday, etc. For the View, I'd like to be able to filter by day of week based on today's date. Can you advice how I can do this?
I don't know of a way to do this in the Views GUI, because taxonomy filters do not offer any date-based options. Instead, you could use custom code that automatically applies a filter to the query. Assuming the term slugs are "monday", "tuesday", and so forth, the following code in functions.php will filter 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.
Hi Christian,
I have tried adding your code via both editor and Code Snippet, but not getting any filter result. Please see the image as attached.
Also, I understand this code filter by only the specified "day-of-week", is that right? What if I want the slug to change based on today's actual day? For example, for today it is to filter by Tuesday. For tomorrow, filter by Wednesday.
Thank you so much and looking forward to your reply.
I have tried adding your code via both editor and Code Snippet, but not getting any filter result.
Let me clarify - you won't notice anything different about the dropdown filter options in wp-admin. In fact there is no need to add a Query Filter (unless you want to use additional filters we have not discussed). The code we added to your PHP file will be invisible in wp-admin, and will be applied programmatically.
What if I want the slug to change based on today's actual day? For example, for today it is to filter by Tuesday. For tomorrow, filter by Wednesday.
This is exactly how it works now. In the code below, we are creating a dynamic term filter based on the current day of the week -
date('l'). So on Monday, the term slug filter is "monday". On Tuesday, the term slug filter is "tuesday", and so on. No changes are necessary for this to work like you have described.
'tax_query' => array(
array(
'taxonomy' => 'day-of-the-week',
'field' => 'slug',
'terms' => strtolower(date('l')),
'operator' => 'IN'
),
),
HI Christian,
I put the code into Code Snippet for functions.php and I got an error No items found. Can you please advice?
Sure, let's try these troubleshooting steps:
- Edit this View and uncheck "Don't include the current page in results", that can cause unexpected problems.
- Go to Toolset > Taxonomies and edit your custom "Day of the Week" taxonomy. Make sure that the slug of this taxonomy is "day-of-the-week". If it is not, you must modify the custom PHP code to match your taxonomy slug.
- Go to Events > Day of the Week so you can see a list of all the terms in the Day of the Week taxonomy. Edit each slug and make sure the slugs are "monday", "tuesday", "wednesday", and so on.
- When this is configured correctly, you should be able to see an archive of Events tagged with each day term at these URLs:
hidden link
hidden link
hidden link
hidden link
hidden link
hidden link
hidden link
- If your taxonomy slug is not "day-of-the-week", the URLs above should be modified to match your taxonomy slug.
Let me know how it goes for you. If this isn't working as expected, I may need to log in to your wp-admin area and take a closer look. I will activate private reply fields for you here.
Hi Christian,
I think the problem is the taxonomy slug. I'm actually using a custom field, not custom taxonomy. The custom field called day of week is stored as a string data type, using a radio form field.
Can you advice what I should do in this case?
I'm confused. What is the custom taxonomy you mentioned here?
In the CPT, my custom taxonomy stores data as day of week
Sorry. It should be the custom field of a CPT. Please forget about the taxonomy. Can you help?
Okay the code above was designed for a taxonomy filter, so please disregard it. Here is the code optimized for a custom field filter:
add_filter( 'wpv_filter_query', 'todays_posts',99,3 );
function todays_posts( $query,$views_settings, $view_id) {
$views = array( 1234, 5678 );
if ( in_array( $view_id, $views ) ){
$args = array(
'relation' => 'AND',
array(
'key' => 'wpcf-yourfieldslug',
'value' => date('l'),
'compare' => '='
)
);
$query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
$query['meta_query'][] = $args;
}
return $query;
}
Replace 1234, 5678 with a comma-separated list of View IDs where you want to apply this filter, and replace 'wpcf-yourfieldslug' with 'wpcf-' + your day of the week field slug. This code assumes your custom field values are "Monday", "Tuesday", and so on.
Hi Christian,
Thanks for the code. I added it to Code Snippet, and still not working as I expected. I understand that is because I have additional filtering criteria in the View, and those were not used when the code is customized. What I need are
Filter by boolean-type field for true/false called weekly-event
Order by int-type field called Priority
I understand that is because I have additional filtering criteria in the View, and those were not used when the code is customized.
The custom code we applied does not affect the orderby attribute of a query, so any orderby attribute applied to the View elsewhere should be respected. The code also maintains any other meta queries that are already applied to the View, so the weekly-event field should still be applied.
At this point, I think it makes sense for me to create a clone of your site so I can investigate in more detail. I can install the Duplicator plugin and begin creating that clone if you agree. I will provide private reply fields here so you can share login credentials.
Please let me know which View I should be looking for, and where I can see that View on the front-end of the site.
You can access the View on the home page at hidden link
Please check the home page now. It looks like the featured todays event banner View wasn't placed on the page correctly, instead the featured-special-events-banner View was placed twice. Also the Content Template applied to the featured todays event banner View was incorrect, maybe this View was cloned from another View and the old Content Template was placed here? Not sure. I replaced that with a new Content Template, and I think things are looking better now. Salsa Mambo Monday is now appearing in the results, which appears to be accurate.