admin管理员组文章数量:1193933
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.
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.
Share Improve this question asked Aug 6, 2022 at 21:33 rafaelfndevrafaelfndev 2272 silver badges10 bronze badges 1 |1 Answer
Reset to default 2WP_Query
provides no such date parameter. While you could query for/store the date of the earliest post on the site and then calculate every date for a particular day of the week in the timespan since then and query based on that criteria, it would be far more efficient to track the publish day of week directly.
This could be accomplished with post meta-data, but for query efficiency it would probably make more sense as a taxonomy with a term for each day of the week. As an added benefit of storing this information in a taxonomy, you'll also get automatic archives and permalinks, which sound as though they might be beneficial here - but you can disable that functionality if not.
Add a hook to look up and set the appropriate day of week term whenever a new course is inserted.
If the archives don't directly fit your filter needs, your filtration query would be just a matter of specifying the day of week taxonomy/term.
本文标签: wp queryWPQueryHow to get all posts of specific days of week by custom field date
版权声明:本文标题:wp query - WP_Query - How to get all posts of specific days of week by custom field date? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738472894a2088692.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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. – Sally CJ Commented Aug 7, 2022 at 0:25