Tell us what you are trying to do?
I am making a view of a content type called a feature. Features have an optional start and end date. I want to set up the view so that features start showing on their start date and stop showing after their end date. However, if no start / end date is set, I want the feature to show.
In my view, I have set up this query filter which basically handles the start / end date part the way I want (although it may be causing some performance problems - my sample page displaying this view won't always load for me) :
Select items with field:
Feature area is a number equal to VIEW_PARAM(area)
AND
Start date is a number lower than or equal NOW()
AND
End date is a string greater than NOW()
But I'm not sure how to do the also-show-if-no-date-set part. What's the best way to go about this? (I'm happy to do something other than the query filter I have set up if there's a better option.)
Hello,
I assume we are talking about you are going to display posts which start and end date field values are not NOT EXISTS.
If it is, it needs custom codes, for example:
1) Use filter hook wpv_filter_query to trigger a PHP function:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
2) Add filters to query, custom fields "start and end date" not NOT EXISTS, see WordPress document:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
meta_compare (string) - Operator to test the 'meta_value'. Possible values are ... 'NOT EXISTS'
Thanks, Luo, the WP documentation was helpful. If anyone finds this looking to do something similar, I ended up adding this as a code snippet under Toolset > Settings > Custom Code, and it seems to be working correctly:
add_filter( 'wpv_filter_query', 'check_visible_dates', 99, 3 );
function check_visible_dates( $query_args, $view_settings, $views_id ) {
if ( $views_id == 126 ) {
$query_args['meta_query'][] = array(
'relation' => 'OR',
array(
'key' => 'wpcf-end-date',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'wpcf-end-date',
'value' => current_time('timestamp'),
'compare' => '>',
),
);
$query_args['meta_query'][] = array(
'relation' => 'OR',
array(
'key' => 'wpcf-start-date',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'wpcf-start-date',
'value' => current_time('timestamp'),
'compare' => '<=',
),
);
}
return $query_args;
}