Skip Navigation

[Resolved] Filter view by custom DATE field shortcode attribute

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

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Rita 5 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#1215096

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.

#1215304

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I need to know here - are you using Events calendar plugin?

if you can share the problem URL where you added the above view and access details it will really help rather asking too many questions here.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1215385

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - the access details you shared with me in the last private reply is not working at this end. I also checked with one of my colleagues and for him it is not working as well.

Can you please send me working access details.

I have set the next reply to private which means only you and I have access to it.

#1215425

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I've added the same code at the "Custom Code" section offered by Toolset :
=> hidden link
(Note: The code should work within your functions.php file as well but this is to show you that Toolset offers a place where you can add your custom code)

More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

I just remove the query filter added to your view's query filter section. There is no query filter now.
=> hidden link

Could you please confirm now it works as expected.

#1215432

Thanks Minesh
Much appreciated!