Skip Navigation

[Resolved] Between Dates

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

Last updated by andrewD-10 4 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#1533125

I am trying to:

Please watch video that explains everything. Link: hidden link

There is an issue with the pick date select without resetting the table and then trying to select the date again.

There is an issue with Between dates as the time is not adjusting for the Max @ 23:99:99 or 11:59:59. The max date is at 00:00:00 which is wrong as it won't show results that occurred after Midnight. The Max date should include the entire date, not just at midnight.

Similar but if this person choose: "after the end date" Link: https://toolset.com/forums/topic/filter-post-with-datetime/ the other option. Link:

#1535209

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I watched the video you shared and as there are multiple issues. We will take issue one-by-one and try to fix that, hope this is OK.

So, the first issue is the date-picker is not working as expected for the max date filter. I would like to know, do you see any javascript error on your browser's console when you click on the max date filter? If yes: Can you please share it.
Or
Can you please share the problem URL where you added your view as well as access details so I can quickly check the things and get back to you.

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

#1536505

Hi Minesh,

The most important topic I need resolved is the 2nd issue. I don't have enough time to take things 1 by 1 as I run a business and my live website has issues.

Please see this link: hidden link

Let me know How we can solve this. You can easily recreate it on your end. Just need the code and where to put the code to account for the full day

After this is resolved we can work on that other issue.

Thank you,
Andrew

#1537199

Minesh
Supporter

Languages: English (English )

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

Ok - thank you for sharing the video and explaining the issue.

The thing is that when you select the date from the date picker it will start with 00:00 so as per your requirement we need to add the 23:59:59 hours:minutes:seconds to the selected date.

To do so, we need to use the view's filter hook: wpv_filter_query
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

You should try to add the following code to your current theme's functions.php file:
OR
You can add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_filter( 'wpv_filter_query', 'func_filter_custom_date_with_fulldaytime', 99, 3 );
function func_filter_custom_date_with_fulldaytime( $view_args, $settings, $view_id ) {
  
    if ( $view_id == 99999) {
		
		  if ( (isset($view_args['meta_query'])) && (!empty($view_args['meta_query'])) ) {
 
                     $target_field = "wpcf-tour-date"; // change this field slug to your field slug
 
           
			foreach ($view_args['meta_query'] as $key => $value):
                                       if ($value['key'] == $target_field){
                                                              $day_start_time =   $value['value'];
                                                              $day_end_time = strtotime('+1 day', $day_start_time ) - 1;
                                                              $filer_index = $key;
				 }
            endforeach;
            
 
            if ( isset($filer_index) ) {
                 
                $view_args['meta_query'][$filer_index] = array('key' => $target_field, 
				                                                  'value' => array($day_start_time, $day_end_time), 
																  'type' => 'NUMERIC', 
																  'compare' => 'BETWEEN' );
																  }       
        }   
	 
	 }
    return $query_args;
}

Where:
- Please change the 99999 with your original view ID
- Change the $target_field variable value to your original field slug

If you need further help with this, please let me know.

#1537809

Hi Minesh,

Thank you for sending, I very much appreciate your help in this.

Unfortunately, I am still not able to get this code to work. I went ahead and made a video showing the code, the view id, the field slug, the view code, and a test. I also tried switching the date field from numeric to string and the slug name wpv-wpcf-bid_date to wpcf-bid_date, but still did not work. Link: hidden link

Please take a look and see if you can find anything I may have done wrong. Also, keep in mind my date filter is filtering "lower than and equal to".

Thank you,
Andrew

#1538285

Minesh
Supporter

Languages: English (English )

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

Can you please send me access details and problem URL as this do not need any debugging I think I can easily fix it on your live site.

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

#1546415

Minesh
Supporter

Languages: English (English )

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

I checked and adjusted the code as required: hidden link

What you need to do is - you need to add the following code to your live site:

add_filter( 'wpv_filter_query', 'func_filter_custom_date_with_fulldaytime1', 99, 3 );
function func_filter_custom_date_with_fulldaytime1( $query_args, $settings, $view_id ) {
   
    if ( $view_id == 41104) {
         
          if ( (isset($query_args['meta_query'])) && (!empty($query_args['meta_query'])) ) {
  
           $target_field = "wpcf-bid_date"; // change this field slug to your field slug
  
             foreach ($query_args['meta_query'] as $key => $value):
                                       if ($value['key'] == $target_field){
                                         
                                         
                                              $day_start_time =   $value['value'];
                                              $day_end_time = strtotime('+1 day', $day_start_time) - 1;
                                                              $filer_index = $key;
                                         
                 }
            endforeach;
             
  
            if ( isset($filer_index) ) {
                  
                $query_args['meta_query'][$filer_index] = array('key' => $target_field, 
                                                                  'value' => array($day_start_time, $day_end_time), 
                                                                  'type' => 'NUMERIC', 
                                                                  'compare' => 'BETWEEN' );
                                                                  }     
            
         
        }   
      
     }
    return $query_args;
}

where:
- You need to change the $view_id which is in this case 41104 with your original view ID of your live site.

Can you please confirm now it works as expected on your live site as well.

#1547405
Screen Shot 2020-03-11 at 10.13.51 AM.png
Screen Shot 2020-03-11 at 10.12.24 AM.png

Hi Minesh,

Ok very good, we are almost there. I see that it now includes the date selected in the filter. However, now the code is formated only to "equal" the date when originally it is supposed to be set to "lower than or equal". Please see screenshots of how the view was originally and the search results given.

You will see that although it is equal to the 10th, it is not showing dates lower than the 10th as well.

Thank you,
Andrew

#1547719

Minesh
Supporter

Languages: English (English )

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

You need to discard that filter setting as we are changing the filter on fly using the code I shared.

We are changing the filter on fly and checking for the selected date from the time where the date start from 00:00:01 to 23:59:59. So, when you select the 10th date as per your request it will try to fetch all the records between 00:00:01 to 23:59:59 from that selected date 10th.

#1548515

Hi Minesh,

I understand that is what the code is now doing; however, I was clear in the beginning of this ticket that I need a code that includes the date search and everything prior to that date. The code you provided is not to my specifications. Please discuss with other coders about this issue as it is very important and does not work as per our spec.

To recap, if I click on March 10th, I would like it to show March 10th and every date before March 10th.

Thank you,
Andrew

#1548521
Screen Shot 2020-03-12 at 8.48.20 AM.png

Additionally, the view that this code is going to also should be only showing posts with bid dates that are "Past". This is now not working and is showing all posts future bid dates and past bid dates.

Please see the screenshot of the code used to achieve this.

Thank you,
Andrew

#1549677

Minesh
Supporter

Languages: English (English )

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

To recap, if I click on March 10th, I would like it to show March 10th and every date before March 10th.
==>
Ok - I understood.

I've deactivated the code snippet you added "pasttable-prefilter" and added a new code snippet "toolset-custom-code" (see scrrenshot: hidden link) where I've adjusted the code as per your requirement as given under:


add_filter( 'wpv_filter_query', 'func_filter_custom_date_with_fulldaytime1', 99, 3 );
function func_filter_custom_date_with_fulldaytime1( $query_args, $settings, $view_id ) {
   
    if ( $view_id == 41104) {
         
          if ( (isset($query_args['meta_query'])) && (!empty($query_args['meta_query'])) ) {
  
                   $target_field = "wpcf-bid_date"; // change this field slug to your field slug
  
             
            foreach ($query_args['meta_query'] as $key => $value):
                     
                      if ($value['key'] == $target_field){
                                         
                               $day_start_time =   $value['value'];
                               $day_end_time = strtotime('+1 day', $day_start_time) - 1;
                               $filer_index = $key;
                                         
                 }
            endforeach;
             
  
            if ( isset($filer_index) ) {
                  
                $query_args['meta_query'][$filer_index] = array('key' => $target_field, 
                                                                  'value' => $day_end_time, 
                                                                  'type' => 'NUMERIC', 
                                                                  'compare' => '<=' );
            }     
            
         
        }   
      
     }
    return $query_args;
}

I can see now when I select the 10th date, it also displays the past date posts as well:
=> hidden link

I do not understand what you mean to say using the following lines:

Additionally, the view that this code is going to also should be only showing posts with bid dates that are "Past". This is now not working and is showing all posts future bid dates and past bid dates.

Is the code above I shared enough for you and displays the expected results?

#1557673

Hi Minesh,
Sorry for late response. Thank you very much for your help in accomplishing this.

As far as the other issue.

The Past Bid Table should only show bid dates that are yesterday and past. Similar to this filter we did it will show the past date and everything before that date. Here is the code to make this happen.

/**
* 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_pasttable($view_args, $view_settings, $view_id) {
$timestamp = current_time('timestamp');
$yesterday = strtotime('-12 hours', $timestamp);
if (in_array($view_id, array(41104))) {
// 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' => $yesterday,
'type' => 'CHAR',
'compare' => '<='
);
}
}

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

This code is no longer working as we have made changes to that table by adding more code above for the filter you just fixed.

New threads created by Minesh and linked to this one are listed below:

https://toolset.com/forums/topic/split-between-dates-past-dates-one-day/

#1557685
Screen Shot 2020-03-20 at 9.53.14 AM.png
Screen Shot 2020-03-20 at 9.53.25 AM.png

For example, the Table in the screenshot is only supposed to show the 19th but instead, it is showing the 20th which is today.

This is because we added a +1 to the filter however is there a way to separate the code so the default code will show the 19th and then the code you just added will select bid date +1?

Right now it seems that the code we just added is affecting another code.

#1561063

Minesh
Supporter

Languages: English (English )

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

Glad to know that the solution I shared works for you. I kindly ask you to mark resolve this ticket.

I suggest you should resolve this ticket and I'll split the ticket where I will offer support for your additional queries. We will communicate with the following split ticket:
=> https://toolset.com/forums/topic/split-between-dates-past-dates-one-day/