Question

Intro: I'm using a weird plugin called The Events Calendar which vastly operates outside of WP functions. For example, I cannot retrieve custom fields with the WP function.

So essentially, my problem is that I need to update a different post type from the custom content posted in an Events Calendar CPT but I can only retrieve it, after the post has fully been saved, not on "save_post" action hook because there is no post meta information at that moment.

So here is my question:

Is there any possibility to introduce an update in post immediately after the save post action? Which options are best recommended

One idea I have is to use another save_post action with less priority that the former that introduces the update function. But not sure if this the post optimal mechanism.

BTW in case anyone has already dealt with The Events Calendar and has a shortcut, what I actually need to do, is, when an Event from The Events Calendar is saved I need to store the event start date in a different CPT.

Was it helpful?

Solution

Modern Tribe has presented several articles to address issues like yours.

Please take a look at their article "Tips for Working With WordPress Actions and Filters" to see if you can track down the do_action() or apply_filters() statement that occurs after a post save or publish event.

I believe you may want add_action('tribe_events_update_meta','wpse_your_function', 10,3), found in /src/Tribe/API.php. Below is their documentation for this one:

/**
 * Allow hooking in after all event meta has been saved.
 *
 * @param int     $event_id The event ID we are modifying meta for.
 * @param array   $data     The meta fields we want saved.
 * @param WP_Post $event    The event itself.
 *
 * @since 4.6
 */
do_action( 'tribe_events_update_meta', $event_id, $data, $event );

You should check it out.

If that works out, your code would be a modified version of what Pat J recommended:

add_action( 'tribe_events_update_meta', 'wpse348671_updated_meta', 10, 3 );
function wpse348671_updated_meta( $event_id, $data, $event ) {    
    // Update/create your 2nd post here.
    //The event start date should be stored in $data['EventStartDate'].    

    // this should work too.
    $start = get_post_meta( $event_id, '_EventStartDate', true )
}

OTHER TIPS

The Events Calendar stores the start and end dates as metadata. The keys are _EventStartDate and _EventEndDate.

You can hook into this using the updated_postmeta action hook:

add_action( 'updated_postmeta', 'wpse348671_updated_meta', 10, 4 );
function wpse348671_updated_meta( $meta_id, $object_id, $meta_key, $meta_value ) {
    if ( '_EventStartDate' == $meta_key ) {
        // Create your 2nd post here.
        //The event start date is stored in the variable $meta_value.
    }
}

This code is untested.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top