Skip Navigation

[Resolved] Disregard Time when filtering a table based off a date field

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

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

Assisted by: Minesh.

Author
Posts
#1640275

Video: hidden link

Hi Toolset,

Below is custom code I got a while back. What it does is filter a table to only show the current bidding projects. This means that the table will only show posts that have a date field that has a date and time (right now) and into the future. This table will not show any posts that have a past date or time.

This is an issue as I want this table to show any and all posts that have a date of today and into the future (disregard time).

For example, the current time is 11:30 am. The current set up will show a post with a date and time of 05-27-2020 12 pm but it won't show a post with a date and time of 05-27-2020 10 am. What I would like is for the table to show any post with a date of 05-27-2020 and forget about the time. Essentially the custom code should not count for the time and only go from 12 am of today and into the future.

Not good: Today and Current Time -> Future
Good: Today 12 am -> Future

Custom Code:

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
function filter_bid_date_default_current_bids($view_args, $view_settings, $view_id) {

if (in_array($view_id, array(40875))) {
// slug of the bid date field
$bid_field_slug = "wpcf-bid_date";

// check for the existing meta query filters
if(!empty($view_args['meta_query'])) {
foreach ($view_args['meta_query'] as $meta_query_arr ) {
if( (!empty($meta_query_arr['key'])) && (isset($meta_query_arr['key'])) ) {
$available_keys[] = $meta_query_arr['key'];
}
}
}
else {
$available_keys[] = '';
}

// if no user filter for the bid date exists add a fixed one
if (!in_array($bid_field_slug, $available_keys)) {
$view_args['meta_query'][] = array(
'key' => $bid_field_slug,
'value' => current_time('timestamp'),
'type' => 'CHAR',
'compare' => '>='
);
}
}

return $view_args;
}
add_filter('wpv_filter_query', 'filter_bid_date_default_current_bids', 99, 3);

#1641167

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL where you added your view as well as access details.

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

#1642919

Minesh
Supporter

Languages: English (English )

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

Thank you for details. Just before I should make any changes to the custom code.

On this page: hidden link

you only want to show posts whose date fall into current date only?
For example:
29th May,2020 00hr 00min 01sec To 29th May,2020 23hr 59min 59sec

You do not want to show feature date - correct? Only the current date posts that falls in 24 hours of the current date?

#1644371

Hi Minesh, regarding your question:

you only want to show posts whose date fall into current date only?
For example:
29th May,2020 00hr 00min 01sec To 29th May,2020 23hr 59min 59sec

You have the right idea, except extend the "TO" to the infinite future.

Such as: "today 00hr 00min 01sec" to "31st Dec, 9999 23hr 59min 59sec"

Reminder that this is for the custom field "bid-date". Thanks!

#1646441

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added and adjusted your code snipped as given under:

// Put the code of your snippet below this comment.
function filter_bid_date_default_current_bids($view_args, $view_settings, $view_id) {
 
    if (in_array($view_id, array(40875))) {
        // slug of the bid date field
        $bid_field_slug = "wpcf-bid_date";
 
		
        // check for the existing meta query filters
        if(!empty($view_args['meta_query'])) {
            foreach ($view_args['meta_query'] as $meta_query_arr ) {
                if( (!empty($meta_query_arr['key'])) && (isset($meta_query_arr['key'])) ) {
                    $available_keys[] = $meta_query_arr['key'];
                }
            }
        }
        else {
            $available_keys[] = '';
        }
 
        // if no user filter for the bid date exists add a fixed one
        if (!in_array($bid_field_slug, $available_keys)) {
          
            $currentDate = date('Y-m-d');
            $currentDate = $currentDate." 00:00:01";
            $currentDateTimestamp = strtotime($currentDate);
          
            $view_args['meta_query'][] = array(
                'key' => $bid_field_slug,
                'value' => $currentDateTimestamp,
                'type' => 'CHAR',
                'compare' => '>='
            );
        }
    }
 
    return $view_args;
}
add_filter('wpv_filter_query', 'filter_bid_date_default_current_bids', 99, 3);

Can you please confirm it works as expected now.

#1647077

Minesh, thank you so much for your help. You are awesome. Tested on the live site and works perfectly!

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