Skip Navigation

[Resolved] Filter posts by custom field date

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

Last updated by matthewL-7 2 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2285635

Hi

I want to filter posts by a custom field but the custom field stores the date in the following format:
2022/02/23 23:00

Not in unix timestamp.

Is it possible to filter the view by this field? I want to filter it so it only shows posts that have this date within the next 2 months, I understand how to do this if it was a unix timestamp but it doesn't work I guess because of this custom date/time format.

#2285777

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Toolset date filters works on Unix timestamp and also Toolset custom date field stores the value as Unix Timestamp.

Can you please share more details how you setup custom field and share admin access details and where you added the filter?

*** 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.

#2286787

Minesh
Supporter

Languages: English (English )

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

You want to add static filter from view to display posts that falls in next two months start from today's date - correct?

#2290513

Yes that’s exactly right Minesh! 😀

How can I do this since the field is not in unix timestamp please?

#2292005

Minesh
Supporter

Languages: English (English )

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

As you can see with your view's "Query Filter" section:
- hidden link

I've added the following Query filter:

Select items with field:
_wpscp_schedule_draft_date is a DATETIME between NOW(), FUTURE_DAY(60)

Can you please confirm it works as expected at your end.

#2295339

Ah yes I did already try this but it doesn't work. I guess because the field is not in Unix timestamp. Its formatted like this:
2022/02/23 23:00

If you look at the view here - hidden link

Offers should be appearing as there are offers with the draft date set to within 60 days.

#2295341

Minesh
Supporter

Languages: English (English )

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

The admin auto-login link is not working. Can you please send me admin access details again and let me see whats going wrong with your setup.

Please share what offers should be included.

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

#2298859

Minesh
Supporter

Languages: English (English )

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

I've added the following filter code to "custom code" section offered by Toolset with code snippet "toolset-custom-code":
=> hidden link

add_filter( 'wpv_filter_query', 'func_custom_date_filter_query', 10, 3);
function func_custom_date_filter_query( $view_args, $view_settings, $view_id ){
  
    if ( 71155 == $view_id ) {
  
            if ( (isset($view_args['meta_query'])) && (!empty($view_args['meta_query'])) ) {
    
             $target_field = "_wpscp_schedule_draft_date"; // change this field slug to your field slug
                 
            foreach ($view_args['meta_query'] as $key => $value):
                      
              if ($value['key'] == $target_field){

                $date_range = explode(",",$value['value']);	
                $start =date('Y-m-d', $date_range[0]);
                $end = date('Y-m-d', $date_range[1]);
                $view_args['meta_query'][$key] = array('key' => $target_field, 
                                                       'value' => array($start,$end), 
                                                       'type' => 'DATE', 
                                                       'compare' => 'BETWEEN' );
              }
            endforeach;            
        }
         
    }
  
    return $view_args;
}

I can see its displaying and showing next 60 days posts:
=> hidden link

#2299883

Brilliant thank you! Working perfectly now.