Skip Navigation

[Resolved] Passing Date Argument to View

This support ticket is created 5 years, 2 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 3 replies, has 2 voices.

Last updated by Minesh 5 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1558529

Tell us what you are trying to do?
I need to filter a view based on a custom Date/Time field by passing the Date/Time argument as Shortcode attribute. What format do I use to pass the timestamp to the view? I have tried several variations such as: start="2020-03-21 14:10:00", etc. None of them return any results.
I am able to pass numeric or text arguments to the view, but have been unable to pass a DATE/TIME argument.

Is there any documentation that you are following?
https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/#controlling-the-filter-with-shortcode-attributes

Is there a similar example that we can see?
Noi

What is the link to your site?
Local site under development

#1559429

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

The thing is that Types stores custom date field value as a UNIX timestamp to the database.

To filter the view using your custom date field, you need to pass either UNIX timestamp value that is the equivalent value of Unix timestamp to your date field.
OR
You can pass the readable date as you are passing currently: start="2020-03-21 14:10:00"
And use the view's filter query hook: wpv_filter_query hook where you should catch the passed start shortcode attribute value and convert it to UNIX timestamp and pass the related custom field filter.

As I understand, you wnat to display posts that is less than or equal to the start date value -that is passed using the view's shortcode attribute correct?

Please let me know with what option you would like to go with and I will guide you accordingly.

#1560685

Hi Minesh - thank you for your reply.
Can you please provide more detail on the query hook method?
Thanks,
Andreas

#1560999

Minesh
Supporter

Languages: English (English )

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

Sure.

You can use the View's API filter hook wpv_filter_query to change the view's query argument on fly.
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example:
- You can add the following code to your current theme's functions.php file
OR
- You can add it to custom code section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_filter( 'wpv_filter_query', 'func_custom_attribute_to_views', 99, 3 );
function func_custom_attribute_to_views( $query_args, $view_settings, $views_id  ) {
    if ( $views_id == 9999   ) {  
         global $WP_Views;
         $attributes = $WP_Views->view_shortcode_attributes;
         
    }
    return $query_args;
}

Where:
- Change the 9999 with your original view ID
- You can find the passed shortcode attribute start with the $attributes variable as shown above.
- You can get the passed attribute and convert it to Unix timestamp and you need to adjust the query argument

Please let me know if you require further assistance.