Skip Navigation

[Resolved] Filtering post by "On This Day" using just the month and day to filter

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Our next available supporter will start replying to tickets in about 8.25 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 3 voices.

Last updated by Nigel 6 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#952446

Hi!
I'm trying create a view like "On This Day". I need to filter post with the date of today but not the year or exact date.
Something like:

On This Day page will show post from:

July 20, 1972
July 20, 1934
July 20, 1981
July 20, 1990
July 20, 1993

I tried to use date filter following others tickets and guides but it doesn't work.

Could you help me please.

#952819

I assume you use the Post Date, not a Field, for this, right?
Is this helpful?
https://toolset.com/documentation/user-guides/filtering-views-query-by-date/

This results in a query filter in the view like:

Select posts whose
Published date is equal to: week:CURRENT_ONE(), day:CURRENT_ONE()

This wil flor example show all posts posted today and this week but no matter what year or month.

#953244

Hi Beda,
I'm using a field. Post date don't work in this case because we uploaded the custom posts with a csv and all have the same day of publish.

there is a way to do this with a date field?

#953548

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Leonardo

The solution described above by Beda works specifically when filtering by the post date, as the underlying query uses a date_query which can handle this, whereas with a date custom field the underlying query is a meta query, which can't.

I don't see how you can modify the query for this to work before the query is run.

The only option I see is for your View to return all posts (i.e. without the on-this-day filters), and for you to then iterate over the query results and remove all posts which are not from this day.

That means using the wpv_filter_query_post_process to manipulate the query results after the query has been run but before the View outputs the (see https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process).

So you'll need code hooked into that filter which uses a foreach loop to iterate over all of the results, gets the custom date field (which is in a UNIX timestamp format), tests whether the date and month match today, and removes the post from the results if not.

You'll also need to update the found_posts and posts_count values, and re-index the results.

You can see an example of doing something similar in this post: https://toolset.com/forums/topic/displays-only-posts-with-less-than-3-children/#post-452734

The test for whether to remove posts from the results is different, which you would need to update for your date tests, but the structure of the code is similar.

Try that, and if you get stuck let me know.