Skip Navigation

[Resolved] Views based off dates

This thread is resolved. Here is a description of the problem and solution.

Problem:

Setup add custom filters using wpv_filter_query filter hook.

Solution:

This is a custom codes problem, for example:

https://toolset.com/forums/topic/views-based-off-dates/#post-1634313

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 13 replies, has 2 voices.

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

Assisted by: Luo Yang.

Author
Posts
#1623275

Hello, we have a view that shows posts based off a custom field date, named Bid Date.
The view shows posts with an "older than today" Bid Date.

Here is what is happening:

1. The view works as intended when the page/view first loads, because we have custom code to filter past Bid Dates.
As seen here in this screenshot: hidden link

2. However, if I use the search filters, it does not respect our custom code about the Bid Dates.
As seen here in this screenshot: hidden link

Do you have any suggestions or ideas to fix this?
Or is there a way to do this without custom coding?

Thank you!

#1623975

Hello,

How do you setup the custom codes you mentioned above?

I suggest you try to follow our document to use filter hook "wpv_filter_query" to setup your custom codes and add custom filters to view:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class.

#1629599

Hi Luo, please see this video about our current situation: hidden link

Thank you. Here is the custom code ToolSet gave us mentioned in the video:

<?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.
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' => '<=' );
                                                                  }     
             
          
        } else {
           
            $bid_field_slug = "wpcf-bid_date";
            $timestamp = current_time('timestamp');
            $yesterday = strtotime('-12 hours', $timestamp);
    
              
            $query_args['meta_query'][] = array(
                'key' => $bid_field_slug,
                'value' => $yesterday,
                'type' => 'NUMERIC',
                'compare' => '<='
            );
         
       }  
       
     }
    return $query_args;
}
#1629811

Thanks for the video, this is a custom codes problem, please provide a test site with the same problem, I need to test and debug it in a live website. Private message box enabled

#1631333

The problem is in your custom PHP codes, I have changed it as below:

<?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.
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' => '<=' );
                                                                  //}  //comment this line   
             
          
        } else {
           
            $bid_field_slug = "wpcf-bid_date";
            $timestamp = current_time('timestamp');
            $yesterday = strtotime('-12 hours', $timestamp);
    
              
            $query_args['meta_query'][] = array(
                'key' => $bid_field_slug,
                'value' => $yesterday,
                'type' => 'NUMERIC',
                'compare' => '<='
            );
         
       }  
       
     }
    }// add this line
    return $query_args;
}

Please test again, check if it is fixed. thanks

#1632241

Hi Luo, I'm still seeing inconsistancies with the search pages. Please see this video for more info: hidden link

To Summarize:
1. The past bid page should only show posts with a "bid date" in the past.
2. The active bid page should only show posts with a "bid date" of today and future.

Thank you.

#1632931

This is a custom PHP codes problem, for example custom PHP code "past-bid-table", there are lots of if/else in it, please elaborate the purpose of your custom PHP codes:
When do you want to query posts which are
- strtotime('+1 day', $day_start_time) - 1
- strtotime('-12 hours', $timestamp)

Thanks

#1633541

Hi Luo, I don't know much about PHP and I believe you wrote that code originally, however:

What we are tying to do is quite simple once you think about it:
1. The past bid page should only ever show posts with LESS THAN 'TODAY' bid dates.
2. The active bid page should only ever show posts with EQUAL TO OR GREATER THAN 'TODAY' bid dates.

And of course let the users do filters on the page to further refine their results, but still respect the "Date" logic above.

I don't know if custom code is needed or not, but I'll let you make that call.

#1634313

I have checked your previous threads:
https://toolset.com/forums/topic/between-dates/
Those custom codes are wrote by Minesh.

I have changed the PHP codes as below:

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($_GET['wpv-wpcf-bid_date'])) && (!empty($_GET['wpv-wpcf-bid_date'])) ) {
			$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' => '<=' );
			} 
		} else {
			$bid_field_slug = "wpcf-bid_date";
            $timestamp = current_time('timestamp');
            $yesterday = strtotime('-12 hours', $timestamp);
    
              
            $query_args['meta_query'][] = array(
                'key' => $bid_field_slug,
                'value' => $yesterday,
                'type' => 'NUMERIC',
                'compare' => '<='
            );
		}  
	}
    return $query_args;
}

The problem is:
You need to check the URL parameter "wpv-wpcf-bid_date" instead of "meta_query" parameter, so it will check if user has set a value in to field "bid date" input. For example:
replace this line from:
if ( (isset($query_args['meta_query'])) && (!empty($query_args['meta_query'])) ) {

With:
if ( (isset($_GET['wpv-wpcf-bid_date'])) && (!empty($_GET['wpv-wpcf-bid_date'])) ) {

Please test again, check if it is fixed.

#1637665

Hi Luo. I just noticed that if a post has a Bid Date of 9:00 AM, but the current time is 10:00 AM, the post will not show up in either Active or Past views.

Can you get the code to ignore the TIME altogether, and only separate the projects on each view by the DATE only?

Thank you.

#1638069

Please elaborate the question with more details, which one is the "post has a Bid Date of 9:00 AM"?
How and where can I see it in front-end?

#1638789

Hi Luo. First let's recap the 2 pages/views:

1. The past bid page should only ever show posts with LESS THAN 'TODAY' bid dates.
2. The active bid page should only ever show posts with EQUAL TO OR GREATER THAN 'TODAY' bid dates.

Now let's imagine some examples. Let's say the exact time now is 5/26/2020 at 9:00 AM.

-- Example 1: If a project has a bid_date of 5/25/2020 at "anytime", it shows up in the Past Bid Page.
(that's good because 5/25 is before 5/26)

-- Example 2: If a project has a bid_date of 5/26/2020 at 9:30 AM, it shows up in the Active Bid page.
(that's good because 9:30 AM is after 9:00 AM)

-- Example 3: If a project has a bid_date of 5/26/2020 at 8:30 AM, it doesn't show up in either page.
(bad. It should show up in the Active Bid page)

Solution: Ignore the "TIME OF DAY", and split the projects up based "DATE" alone.

Thank you.

#1639321

According to our support policy, we don't provide custom codes support:
https://toolset.com/toolset-support-policy/

You can adjust the timestamp valie of PHP codes to what you want, for example:
change this line:
$yesterday = strtotime('-12 hours', $timestamp);
To:
$yesterday = strtotime('-36 hours', $timestamp);

More help:
hidden link
time
A date/time string.

#1640271

My issue is resolved now. Thank you!

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