Hi there
I would like to use a simple view of a post type (Event) and filter the view by one of the post type's custom fields (wpcf-event-date-start). I understand that the Toolset date field is rendered as a timestamp and that we need to convert it to call the date as a shortcode attribute.
So I have tried using this advice from José Arcos: https://toolset.com/forums/topic/passing-the-date-as-shortcode-attribute/
But... I can't get it to work...
Here is an example to display all 'Events' with a start date of 22 March 2019.
VIEW SETTINGS FOR: event calendar entries
- Post Type: Event
- Sorted by 'post date' 'Ascending'
- Display 'No Limit'
- Filter by shortcode attribute: event date start is a number equal to VIEW_PARAM(day)
VIEW SHORTCODE IN CONTENT TEMPLATE
[wpv-view name="event-calendar-entries" day="22/3/2019"]
FUNCTION SNIPPET
function convert_day_to_timestamp( $query_args, $view_settings, $view_id ) {
global $WP_Views;
$index = 0;
if( $view_id == 83230 ) {
$view_shortcode_attributes = $WP_Views->view_shortcode_attributes;
$day = $view_shortcode_attributes[0]['day'];
$day_splitted = explode( '/', $day );
$start_time = mktime( 0, 0, 0, $day_splitted[1], $day_splitted[0], $day_splitted[2] );
$end_time = mktime( 23, 59, 59, $day_splitted[1], $day_splitted[0], $day_splitted[2]);
if( isset( $query_args[ 'meta_query' ] ) ) {
$index = count( $query_args[ 'meta_query' ] ) + 1;
}
$query_args[ 'meta_query' ][ $index ] = array (
array (
'key' => 'wpcf-event-date-start',
'value' => $end_time,
'type' => 'NUMERIC',
'compare' => '<='
),
array (
'key' => 'wpcf-event-date-start',
'value' => $start_time,
'type' => 'NUMERIC',
'compare' => '>='
),
'relation' => 'AND'
);
}
return $query_args;
}
add_filter( 'wpv_filter_query', 'convert_day_to_timestamp', 99, 3 );
CONSIDERATIONS
The date custom field is a date and time field not just a date field... Does this matter?
I would really appreciate it if someone can point me in the right direction. For sure, something in the above settings is wrong... But I can't figure it out... It would be REALLY great if this way to display views is possible.