Skip Navigation

[Resolved] conditional display based on last of many related event dates

This support ticket is created 5 years, 8 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by christinaB-2 5 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#1251353

Tell us what you are trying to do?
We are an animal rescue that hosts many adoption events that are attended by many cats. So we have a many-to-many relationship between post types Cats and The Events Calendar Events.

The following conditional code works successfully when a Cat is only related to a single event:
[wpv-conditional if="( $(_EventEndDate).id(tribe_events) gt '[current-date]' )" debug="true"]
Visit me at an adoption event!
[/wpv-conditional]

Note that the shortcode [current-date] is a custom shortcode that returns the current datetime in the same format as event calendar dates.

In this case, where Cats are related to only a single event the date comparison works the way it is supposed to and cats that are related to past events do not show the conditional text and cats that are related to future events show "Visit me at an adoption event."

The problem is that Cats that are related BOTH to a past event AND to a future event do not show the conditional code. In that case the conditional compares the date of the past (first) event to the current date. I would like it to compare the date of the future (last) event to the current date. How do I do that?

Is there any documentation that you are following?
I can't find any.

Is there a similar example that we can see?
I can't find any.

What is the link to your site?
Here's a link to a cat on our staging site that is related to both a past and future event. Scroll to the bottom of the page to see the conditional debug code: hidden link

#1251547

Hi Christina,

Thank you for contacting us and I'd be happy to assist.

The challenge here is that the "The Events Calendar" plugin doesn't store the date values, in a standard UNIX timestamp format, that Toolset uses for date field comparisons and filters.
( https://toolset.com/documentation/user-guides/date-filters/ )

To make this work, my first recommendation would be to add a new date type custom field to the "Events" post type, so that each event's end date is available in the standard UNIX timestamp format.

1. Add a new date type custom field to the Events post type, using Toolset, e.g. "TS Event End date".

2. You can use "save_post" hook to programmatically update the value of this new custom field, matching the date from the Event Calendar plugin's "End Date" field ( _EventEndDate ):
( ref: https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/ )


function auto_update_date_field( $post_id ) {
   
    if ( get_post_type( $post_id ) == 'tribe_events' ) {
        
        $event_date = get_post_meta( $post_id, '_EventEndDate', true );

        if(!empty($event_date)) {

        	$date_obj=date_create($event_date);

        	update_post_meta( $post_id, 'wpcf-end-date-field-slug', date_format($date_obj,"U") );
        }
        
    }
        
}
add_action( 'save_post', 'auto_update_date_field', 99 );

The above code can be added into the active theme's "functions.php" file and please replace "end-date-field-slug" with the actual date field slug from step 1.

As a result, when you'll enter the end date for any new or existing event in the Event Calendar's date field and save/update the event, its value will be automatically saved into the Toolset's end date field, too.

3. Next, you can create a new view to show only the last related event to a cat, based on the end date field.
( screenshot: hidden link )

Please note in the screenshot how the view is set to only show one post, which is ordered in descending order, with respect to "TS Event End date" field from step 1.

4. In the "Query Filter" section, also include a relationship filter to show only those events which are related to the current cat page.
( screenshot: hidden link )

5. In that view's content template, you can include a simple conditional block, to check if the current event's end date has lapsed or not.


[wpv-conditional if="( $(end-date-field-slug) gt 'TODAY()' )"]
Visit me at an adoption event!
[/wpv-conditional]

6. You'll insert this view in your single Cat pages so that this adoption text/button can be shown only when there is any future event available.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1255369

Thanks, Waqar. Your suggestion is working to allow conditional display based on the last of many related events. Much appreciated.

The problem now is that the view output is wrapped in <div id="wpv-view-layout- ... and that added <div> is breaking the display. (I said we were displaying text to keep it simple, but we are actually displaying an icon that needs to lay out nicely next to other icons.) Is there any way to output the raw view content, without this wrapping div?

#1256523

Hi Christina,

Thanks for the update and glad that my suggestion helped.

To disable the wrapping container div from the view's output, you'll find a checkbox "Disable the wrapping DIV around the View", just above the "Output Editor" section.
( example screenshot: hidden link )

regards,
Waqar

#1271813

My issue is resolved now. Thank you!