Skip Navigation

[Resolved] Passing the date as shortcode attribute

This support ticket is created 6 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.

This topic contains 2 replies, has 2 voices.

Last updated by maksimG 6 years, 9 months ago.

Assisted by: José Arcos.

Author
Posts
#623588

Hello
I have "competitions" as custom post type, and each has DateBegin and DateEnd as custom field Datetime type of

I need to filter the competitions, which are happens in particular day. The day is supposed to be passed to the view as shortcode attribute.
Something like

[wpv-view name="listcomps" day="01/01/2018"]

Could you please advise what type of filter should I use in view to sucseed
Thank you

#624270

Hello, thank you for contact us. I will try to do my best to help you.

The date fields are stored as Timestamp in the database, this is an integer number like 1520848854. So, this is not a kind of filter that you can easily create with Toolset, but you can add some custom code that will help you to filter the view with the desired parameter passed as an argument in the shortcode. You can use this code and paste it into your functions.php file:

/**
 * Convert the day parameter to Timestamp for Events view
 */
function convert_day_to_timestamp( $query_args, $view_settings, $view_id ) {
	global $WP_Views;
	$index = 0;
	
	if( $view_id == 7 ) {
    	$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-end-date', 
				  'value' => $end_time,
				  'type' => 'NUMERIC',
				  'compare' => '<='
				),
				array (
				  'key' => 'wpcf-start-date',
				  'value' => $start_time,
				  'type' => 'NUMERIC',
				  'compare' => '>='
				),
			  'relation' => 'AND'
		);
	}
	return $query_args;
}
add_filter( 'wpv_filter_query', 'convert_day_to_timestamp', 99, 3 );

You might need to change the following data:

- In the 8th line, you would need to change the number 7 to your View ID, you can find that, going to your view, and checking the URL, after the parameter view_id.
- In the 19th and 25th line, under the option 'key', you would need to change the key name to your custom field name. This is the prefix 'wpcf' and your custom field slug.

Please let me know if this solves the issue or if you need further details.

Have a good day.

#628705

Hello
Thank you for reply, but I think I find kind of elegant solution. Here we go

1. I ve made a wrapper-shortcode

function DayView($params, $content = null) {
	$datef =  strtotime($params['daydisplay']) + 86400-1;
        $dates =  strtotime($params['daydisplay']);
        $params['postparentid'] . '"]');
	$shortcodestr = '[wpv-view';
	foreach ($params as $key => $value) {
                  $shortcodestr.= ' ' . $key . '="' . $value . '" ';
	}
	$shortcodestr.= '" datef="'.$datef.'" dates="'.$dates.'"]';
	return do_shortcode($shortcodestr);
}
add_shortcode('day-view','DayView');

and then I could use it like

           [day-view name="my-view-name" daydisplay="12-04-2018" viewparam1="261" viewparam2="abcd"]

and in view the date filter looks like

Select items with field: 
     Finish is a UNSIGNED greater than or equal VIEW_PARAM(dates)	
AND 
     Start is a UNSIGNED lower than or equal VIEW_PARAM(datef)