Skip Navigation

[Resolved] Show posts not older than X weeks

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

Problem: I would like to create a Query Filter to show the most recent posts beginning 8 weeks ago and continuing to the present. I have used the post date filter feature to specify Weeks: PAST_ONE (8). That seemed to work well until the beginning of the new year, when the filter stopped showing posts from the previous year. So the filter only seems to work correctly when the date 8 weeks ago was in the same calendar year as today.

Solution: Remove the post date Query Filter in the view editor. Add a custom code snippet to filter by post_date using any PHPstrtotime-compatible date string:

function tssupp_recent_8_weeks($view_args, $view_settings, $view_id)
{
    $view_ids = array( 123, 456 ); // Comma-separated list of View IDs
 
    if ( in_array($view_id, $view_ids) ) {
 
        $view_args['date_query'] = array(
          array(
            'column'  => 'post_date_gmt',
            'after'   => '8 weeks ago'
          )
        );
    }
    return $view_args;
}

Change 123, 456 to be a comma-separated list of View IDs where you want to apply this date filter. If you want to filter by a different start date, change 8 weeks ago to any strotime-compatible date string representing the start date.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://www.php.net/strtotime

This support ticket is created 4 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
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 marcialB 4 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1496601
toolset.PNG

Hi there

I set up a filter that only shows posts that are not older than 8 weeks. You can see my filter attached.

The filter works fine, but not during the period from one to the next year. So currently my view with the filter doesn't show any results. If I change the time to let's say 4 weeks, it works (since that period is fully in 2020). I also tried it with months and days - but there is the same.

How can I achieve that the filter also works in my case?

Thanks for your help.

#1497113

Hi, there's not a simple way to configure this type of dynamic post date filter in wp-admin but I can provide a custom code snippet that will achieve the same effect. Please delete the post date query filter from the View editor in wp-admin, then add the following code in your child theme's functions.php file or to a new code snippet in Toolset > Settings > Custom Code:

function tssupp_recent_8_weeks($view_args, $view_settings, $view_id)
{
    $view_ids = array( 123, 456 ); // Comma-separated list of View IDs

    if ( in_array($view_id, $view_ids) ) {

        $view_args['date_query'] = array(
          array(
            'column'  => 'post_date_gmt',
            'after'   => '8 weeks ago'
          )
        );
    }
    return $view_args;
}

Change 123, 456 to be a comma-separated list of View IDs where you want to apply this date filter. You can also adjust 8 weeks ago using a strototime compatible string, as defined in the PHP docs here: https://www.php.net/strtotime

Let me know if this does not solve the problem and I can take a closer look.

#1497589

Thanks a lot. Seems to work nicely!