Skip Navigation

[Resolved] Query filter: Posts modified within the last 4 weeks

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to query their posts that were modified in the last 4 weeks using views.

Solution:

In order to do this you will need to use the view wpv_filter_query hook.

Have a look at the hook example below.

function tssupp_filter_query($view_args, $view_settings, $view_id)
{
    $view_ids = array( 123, 456 ); // Edit for View IDs to apply to
  
    if ( in_array($view_id, $view_ids) ) {
   
        $view_args['date_query'] = array(
            'after'     =>  '4 weeks ago',
            'column'    =>  'post_modified'
        );
    }
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

Add this to your Toolset custom code section in Toolset->Settings -> Custom Code and ensure that you've clicked on activate after saving.

You will need to change the 123,456 to the ID of your view that you want this hook to apply to.

This support ticket is created 3 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 2 replies, has 2 voices.

Last updated by matthiasK-3 3 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1938367
screenshot query filter.jpg

I want to create a list showing all posts that are modified within the last 4 weeks. So I created a view and tried to define a suitable query filter. I checked the document "Filtering Views Query by Date" but I didn't succeed.

I tried "modified after or equal to week past 4" but that shows me all posts.

Please give me the right setting!

#1938409

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matthias,

Thank you for getting in touch.

Unfortunately you won't be able to perform this task using the query filters on the backend as they are currently not working as intended.

There is a workaround to this using the views filter hook to filter the results. Here is the code below.

function tssupp_filter_query($view_args, $view_settings, $view_id)
{
    $view_ids = array( 123, 456 ); // Edit for View IDs to apply to
 
    if ( in_array($view_id, $view_ids) ) {
  
        $view_args['date_query'] = array(
            'after'     =>  '4 weeks ago',
            'column'    =>  'post_modified'
        );
    }
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

Add this to your Toolset custom code settings in Toolset-> Settings -> Custom Code and ensure that it is activated. Replace the 123 and 456 with the ID of your view.

If you're unsure of what the ID of your view is you can go to Toolset -> Views and get the ID from the id column. If you're not seeing the Views menu you will need to enable it by going to Toolset -> Settings and scroll to Editing Experience and enable the 3rd option.

Once you've done this just refresh the page and you should now see the option for views.

Please let me know if this helps.
Thanks,
Shane

#1938629

The workaround works fine for me. Hope that the date query will be improved in future updates.
Thank you!