Skip Navigation

[Resolved] Filtering a view with post only from last 4 months

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

Problem:
The issue here is that the user wanted to filter their view for posts from the last 4 months.

Solution:

The best way to do this is by using the filter below.

function tssupp_date_filter( $query_args, $view_settings, $view_id ){
   
    if ( 123 == $view_id ) { // Edit View ID
        $query_args['date_query'] = array( 
            array(
                'column'    =>   'post_date_gmt',
                'after'     =>   '4 months ago'
            )
        );
    }
   
    return $query_args;
}
add_filter( 'wpv_filter_query', 'tssupp_date_filter', 101, 3 );

Add this code to your Toolset custom code section at Toolset->Settings->Custom Code ensuring that its activated.

Replace 123 with the ID of your view.

This support ticket is created 3 years 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 8 replies, has 2 voices.

Last updated by Franco Calcagni 3 years ago.

Assisted by: Shane.

Author
Posts
#2228117
date.jpg

We created a view and we need to filter the posts only with publishing date only after the last 4 months.
We already created a filter using the parameters in the enclosed screenshot but what happen when we will have the last 4 months in between 2 years (i.e. in January the 4 latest months will spread also in the former year).

Any help on this?

Thanks
Franco

#2228213

Shane
Supporter

Languages: English (English )

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

Hi Franco,

The best way to resolve this is by using a date query as our date query filters can be inconsistent at times. Add the following to the Toolset custom code settings at Toolset -> Settings-> Custom Code and activate it. This should ensure that you're only getting the posts that were published after 4 months ago.

function tssupp_date_filter( $query_args, $view_settings, $view_id ){
  
    if ( 123 == $view_id ) { // Edit View ID
        $query_args['date_query'] = array( 
            array(
                'column'    =>   'post_date_gmt',
                'after'     =>   '4 months ago'
            )
        );
    }
  
    return $query_args;
}
add_filter( 'wpv_filter_query', 'tssupp_date_filter', 101, 3 );

Replace 123 with the ID of your view.

Please let me know if this helps.
Thanks,
Shane

#2228827
ticker.jpg

Hi Shane, I did what you suggested but the filter seems not working, I enclose a JPG with all settings and script that I created and as you see the ticker shows posts with a date previous than 4 months from today

Please let me know what could be the reason.

thanks

Franco

#2228953

Shane
Supporter

Languages: English (English )

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

Hi Franco,

Would you mind allowing me to have admin access to the website so that I can have a more detailed look at this for you ?

Please where applicable please provide me with a link to an example page where I can see the issue.

I've enabled the private fields for your next response.

Thanks,
Shane

#2229087

Hi Shane, you can login to the admin area of the website with the following credentials

Please do not update plugins or themes since we do not have a backup copy of the website ( checked the backup checkbox just because it is the only way to send you the message).

The page where the view is placed is:
hidden link

Thanks

Franco

#2229241

Shane
Supporter

Languages: English (English )

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

Hi Franco,

I checked the site for you but it appears that everything is working as intended.

I'm not seeing any issues at all with the date filter. All the correct values are being returned.

Thanks,
Shane

#2229275
post.jpg

Hi Shane,
if you look at the following URL
hidden link

you notice that the ticker elements appear also before 4 months from today (I still see posts from June 24 and earlier (see screenshot). those post shall not appear at all since the ticker shall display only the latest 4 months posts

Franco

#2229285

Shane
Supporter

Languages: English (English )

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

Screenshot 2021-11-24 at 2.23.49 PM.png

Hi Franco,

Perhaps view the page in a different browser as this is what i'm seeing. See Screenshot.

I removed the conveyor div temporarily to illustrate what i'm seeing. Notice there are no posts that are breaking query filter specifications.

Thanks,
Shane

#2229625

My issue is resolved now. Thank you!

Franco