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
  • You could use a filter hook to add a custom SQL which looks like 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 a day_of_event taxonomy and add Monday 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
Add a comment  | 

1 Answer 1

Reset to default 2

WP_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