Skip Navigation

[Resolved] Filter posts by date

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

Problem:
Filter posts by date (default post date)

Solution:
If you want to display exactly 7 days old posts and would like to compare the post date you need to use the view's filter hook: wpv_filter_query

You can find the proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/filter-posts-by-date-2/#post-1241738

Relevant Documentation:
=> https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/
=> https://toolset.com/documentation/user-guides/views-filters/

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by David Gimenez 4 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1241698

Hi.

I want to show all post published in last eight days. I tried several combinations but none of them show the results in the right way.

I tried select posts with published date is after or equal day past_one 8. I tried other several combinations.
Also between day current one and day past_one 7.

The results are always wrong-

Can you help me please?

#1241738

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - if you want to display exactly 7 days old posts and would like to compare the post date you need to use the view's filter hook: wpv_filter_query

You should try to add the following code to the "Custom Code" section offered by the Toolset Or to your theme's functions.php file:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_filter( 'wpv_filter_query', 'func_one_week_old_posts', 10, 3);
function func_one_week_old_posts( $query_args ,$view_settings, $view_id) {
    if ($view_id == 99999 ) {
        $query_args['date_query'] = array(
            'column' => 'post_date',
            'after'  => '7 days ago',
        );
    }
    return $query_args;
}

Where:
- Replace 99999 with your original view ID.
- Replace the 7 (as per your required days) if needed

More info:
=> https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/
=> https://toolset.com/documentation/user-guides/views-filters/

#1241750
Captura de pantalla 2019-05-09 a las 16.23.59.png

Hi Minesh.

It's not working. The view is 1366. I'll we add later aldo view 1368 (2 views), but with this one is listing a lot of days, (not last seven days (from 9 may - 2 may).

add_filter( 'wpv_filter_query', 'func_one_week_old_posts', 10, 3);
function func_one_week_old_posts( $query_args ,$view_settings, $view_id) {
    if ($view_id == 1366) {
        $query_args['date_query'] = array(
            'column' => 'post_date',
            'after'  => '7 days ago',
        );
    }
    return $query_args;
}

I attach an screenshot that the post that must be listed, but list a lot of

#1242011

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - can you please share problem URL where you added the view and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1242224

Hi again Minesh. I think that's all works fine. The query in the view had filters with date. I deleted that filters and now the query with your code works perfect!

Thanks a lot for your help.

However i have a question. If I need to query with date filters... Must I do it with functions? Or can I do it with the filters of the view?

#1242226

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - its really hard to answer because it totally depends on your requirement. But we have more accurate control for filters which should be applied to default post date using the wpv_query_filter hook.

#1242746

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.