Skip Navigation

[Resolved] View query filter by date custom field not working

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

Last updated by Shawn 2 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2380745
Screen Shot 2022-06-08 at 5.39.33 PM.png

I have a custom date field assigned to posts that has been saved to the database in this format:

2022-06-01

I'm trying to use a query filter in a view to exclude posts with this custom field set to future dates (for example, the custom field date may be set to two weeks from now). For this view I only want to show posts where that custom date field is today or earlier. I'm attaching a screenshot of the view filter I tried, but it returned no results. I've tried other data formats in the filter but nothing is working. Is there a way to accomplish this?

#2381021

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I just wanted to confirm is the date custom field you are talking about is created using Types plugin or the custom field is created using third party plugin that stores the date value in database as date 2022-06-01 ?

Toolset custom date field stores its value to database as UnixTimestamp.

If you can share admin access details and tell me where you added your view with problem URL I'm happy to look at it and check what could be the possible solution I can share with you once I review your current setup.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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.

#2382589

Shane
Supporter

Languages: English (English )

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

Hi Shawn,

Unfortunately Minesh is off sick today but should be back on Monday to continue assisting you with this one.

Thanks,
Shane

#2383897

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Shawn

Minesh is still off, so let me step in here.

There isn't a way to directly enter a filter for the date format you are working with, i.e. based on yyyy-mm-dd.

You have two options. One is to register a custom shortcode that will return the current date in the same format. You can then set up the Query Filter to treat the field as a string, where the value to compare comes from a shortcode attribute, and where you add that shortcode attribute (with its value coming from your custom shortcode) to the wpv-views shortcode used to insert the View.

Given that this involves writing some custom code, it may just be easier to apply the filter directly via code, using the wpv_filter_query hook (https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query).

Here's an example that should do what you need (it assumes you are not using any other custom field filters on the same View). You need to edit the IDs of the View(s) this should be applied to.

function tssupp_filter_query($view_args, $view_settings, $view_id)
{

    if (in_array($view_id, array( 123, 234 ))) { // Edit View IDs

        $view_args['meta_key'] = 'nm_last_shared_ig';
        $view_args['meta_value'] = date("Y-m-d");
        $view_args['meta_compare'] = '>=';
    }

    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);
#2383943

Thank you SO MUCH Nigel, the view filter is exactly what I needed. I remembered that there was a way to modify the View's query via a filter but I didn't know the particulars. I just needed to modify the comparison to be "<=" instead of ">=" and it worked like a charm.

Thanks again!