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!
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.
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;
}
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
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
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.
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
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.
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.
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.
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?
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.
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.
My issue is resolved now. Thank you!