I have a filter on frontend with days of weeks, and I need show only posts that are published on especific day (or days).
The date are on custom field day_of_event
(the filter is based on this field, not of post published date), and all dates are on date format, like "20220806".
When the user send filter "Sunday" on frontend, something like:
$filter = ['sunday'];
I need filter all posts that are published with custom fields "day_of_event" that match with specific day "Sunday".
Example posts:
Post ID | Name | Date of custom field | Date published 1 | Event test | 20220806 -> Sunday | 20220804 -> Friday 2 | Another event | 20220808 -> Monday | 20220801 -> Monday 3 | Welcome event | 20220816 -> Twersday | 20220805 -> Saturday 4 | Course event | 20220808 -> Monday | 20220807 -> Sunday 5 | Finishing event | 20220724 -> Sunday | 20220622 -> Wednesday 6 | How to make a app | 20220717 -> Sunday | 20220622 -> Wednesday 7 | How to make a website | 20220621 -> Sunday | 20220622 -> Thursday
If the user send $filter = ['sunday']
I got posts 1, 5, 6, 7.
If the user send $filter = ['monday']
I got posts 2, 4.
If the user send $filter = ['monday', 'twersday']
I got posts 2, 3, 4.
How I can do this with WP_Query? To be more specific, I'm already using another filters with meta in the archive template, and I need use filters here:
add_action( 'pre_get_posts', function ( $q ) {
if(!is_admin() && $q->is_main_query()) {
if($q->is_post_type_archive('courses')) {
$q->set(
// ...
);
}
}
});
Thanks.
AND ( DAYNAME(wp_postmeta.meta_value) = 'Monday' )
, but I agree with @bosco regarding using a taxonomy/category (to group the posts based on the event's day), so for example, you could create aday_of_event
taxonomy and addMonday
as a term, then assign it to posts 2 and 4, and use a taxonomy query to find posts in that term. You'd notice a performance improvement, particularly if your dataset is already large, or when it grows larger.