Skip Navigation

[Resolved] Query Filter by date not working

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

Problem: I would like to display a list of posts that were published in the last 2 months, but I can't get the Query Filter to work correctly.

Solution: Month range filters like this aren't simple to add in wp-admin but I can provide a custom code snippet that will help. First, please remove the post date filters from your View. Then add the following code to your child theme's functions.php file, or create a new snippet in Toolset > Settings > Custom code:

add_filter( 'wpv_filter_query', 'get_past_two_months_posts', 10, 3);
function get_past_two_months_posts( $query_args ,$view_settings, $view_id ) {
    $views = array( 12345 );
    if (in_array($view_id, $views)) {
        $query_args['date_query'] = array(
            'column' => 'post_date',
            'after'  => '2 months ago',
        );
    }
    return $query_args;
}

Replace 12345 with the numeric ID of this View.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

This support ticket is created 5 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by barakl 5 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1178491
2019-01-06_13-25-24.png

I have this site:
hidden link

please login with:
FSE_demo
A123456Bc

then go to: hidden link

you can see the post slider in the right side.

I need to show only posts from 2 month back - but exactly 2 month back = if today is 6.1.2019, I need to show all post from 6.11.2018. and I tried all the options and each one of them show me the wrong posts.

see the attached image how I set it now. but next month it will not work

can you write me what do I need to set to present the last 2 month posts?

#1178607

Hi, month range filters like this aren't simple to add in wp-admin but I can provide a custom code snippet that will help. First, please remove the post date filters from your View. Then add the following code to your child theme's functions.php file, or create a new snippet in Toolset > Settings > Custom code:

add_filter( 'wpv_filter_query', 'get_past_two_months_posts', 10, 3);
function get_past_two_months_posts( $query_args ,$view_settings, $view_id ) {
    $views = array( 12345 );
    if (in_array($view_id, $views)) {
        $query_args['date_query'] = array(
            'column' => 'post_date',
            'after'  => '2 months ago',
        );
    }
    return $query_args;
}

Replace 12345 with the numeric ID of this View, and check the results on the front-end of the site.

#1180160

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.