Skip Navigation

[Resolved] Filter posts by past week doesn't work

This support ticket is created 5 years, 10 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.

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 2 voices.

Last updated by Nigel 5 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#1238420

I am trying to:
Filter posts to show only posts from the last 7 days. I've tried almost every combination and nothing works - but this is what i think I'm supposed to be using:
Published date is after or equal to: week:PAST_ONE(1)

Nothing seems to work. I can get it show the right posts, but then it also shows old posts

Link to a page where the issue can be seen:

It's just in a normal view

I expected to see:

ONLY posts from the last 7 days (including today)

Instead, I got:

Posts from the last 7 days as well as much older posts

#1238514

Nigel
Supporter

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

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

Hi Lucy

Although there are a lot of documented options for date-based filters, a simple "within the last week" type filter isn't possible without some custom coding.

We are currently in week 18. The week: PAST_ONE(1) refers to week 17. But not just of 2019, but of any year. You could also add the current year, but then you hit problems at the start of the year.

You can use the wpv_filter_query hook to modify the arguments of your View (https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query).

Try adding the following code (as a snippet in Toolset > Settings > Custom Code):

function tssupp_filter_query($view_args, $view_settings, $view_id)
{
    if (in_array($view_id, array( 123 ))) { // Edit and replace 123 with your View ID (or IDs as a comma separated list)
 
        $view_args['date_query'] = array(
            'after'     =>  '1 week ago',
            'column'    =>  'post_modified'
        );
    }
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

We have an internal ticket to improve date filters but I don't think it is getting much attention, when for such a simple use case it probably should, and you can add your voice for such a feature here: https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

#1238826

Thanks so much for your help Nigel - I worked around it for now by adding the year as a constant to the views, but you're right, it's not ideal.

Can you please explain for me does the week: PAST_ONE refer to weeks from Monday to Sunday? Or just the last 7 days?

Thanks again for your help

#1239016

Nigel
Supporter

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

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

This is an example of the generated query:

[date_query] => Array
        (
            [0] => Array
                (
                    [week] => 17
                    [compare] => >
                    [column] => post_modified
                )

            [relation] => AND
        )

It literally means the week number of the year, and we are currently in week 18.