Skip Navigation

[Resolved] View with Posted Date to be yesterday and -3 days

This support ticket is created 3 years, 6 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)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by andrewD-10 3 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#1797963

Hi Toolset,

I would like one of my views to show posts that were published yesterday (before today at midnight) and between 3 days ago.

Video: hidden link

I may be confused on how to use the query filter; it seems I am doing something wrong.

Thank you,
Andrew

#1799437

It's best to use "between" instead of "equal to" here. For the last (n) days posts, you would want to set up the query filter to find posts publish date between days ago (n) and days ago (0). For yesterday - (3) days ago, you would want to set up the query filter to find posts between days ago (3) and days ago (1). Does this setting give you the results you expect?

#1800701

Hi Christian,

Thank you for your help thus far.

It seems to work ok, but still some issues. Although it is showing posts from 3 days ago it then skips to a different month and continues. This video will show it better: hidden link

I think it might still be looking for a day of the week rather than a past day.

Thank you,
Andrew

#1800705
Screen Shot 2020-10-05 at 1.18.32 PM.png

I see, I think you should change "Day" in the Query Filter settings to "Day of the Year" to achieve something more like what you expect.

#1802113

Hi Christian,

That worked perfectly.

Now for another view, would you help me to show (-1) days post date and everything below?

Thank you,
Andrew

#1802169

In retrospect I think this still needs some tweaking to handle cases in the original View where:
- Some posts are more than a year old
- And/or the current date is January 1 - January 4
The GUI doesn't really give you the ability to handle all the little edge cases. I think this is a good opportunity to use a filter that allows more flexibility and handles both the original and new View queries. Here are two custom code snippets that can be applied in your child theme's functions.php file, or in a new code snippet in Toolset > Settings > Custom Code tab:

function tssupp_between_two_n_days_ago($view_args, $view_settings, $view_id)
{
    $view_ids = array( 123, 456 ); // Apply the 3-1 days ago filter to these View IDs

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

        $view_args['date_query'] = array(
            'after'     =>  '3 days ago',
            'before'    =>  '1 day ago',
            'column'    =>  'post_date'
        );
    }
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_between_two_n_days_ago', 99, 3);


function tssupp_before_n_days_ago($view_args, $view_settings, $view_id)
{
    $view_ids = array( 234, 567 ); // Apply the older than yesterday filter to these View IDs

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

        $view_args['date_query'] = array(
            'before'    =>  '1 day ago',
            'column'    =>  'post_date'
        );
    }
    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_before_n_days_ago', 99, 3);

- Change 123, 456 to a comma-separated list of View IDs where you want to apply the "between 3 days ago and yesterday" filter.
- Change 234, 567 to a comma-separated list of the View IDs where you want to apply the "yesterday and older" filter.

Then you can remove the post date Query Filters from the Views where you want to apply these custom date filters. Let me know if this isn't working as expected and we can take a closer look.

References:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

#1805017

Thank you Christian! I didn't use the code but instead used the query filter for posted date with the following condition: publish date is before - Day - Current one.

I will see how this works for now and use the code for future reference 🙂

Thank you,
Andrew

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