[+] Dynamic date filters for published date such as “published in the last 48 hours”

You can insert Query Filters for published or modified date which are good for fixed dates (e.g. since 1 January 2018) but less useful for relative ranges such as “published this week”.

The date_query argument in WP_Query allows for very flexible date comparisons, and a filter to only include posts updated in the last 48 hours for example can be applied using the wpv_filter_query API filter like so:

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'     =>  '48 hours ago',
            'column'    =>  'post_modified'
        );
    }
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments