Short:
I want to change the post's slug (for permalink purposes) upon saving, and use data I just entered in the post editor to modify it.
Long:
I am currently using the Tribe Events Calendar plug-in, and want to create multiple events with the same title, so to make sure the permalinks are unique, I add the event date (which is stored alongside the post) to the end of the slug on saving the event (post).
What I have done so far
I have the permalink update working by using the save_post
hook:
function change_event_slug_on_save( $post_id ) {
if ( ! wp_is_post_revision( $post_id ) && tribe_is_event($post_id) ) {
// verify post is not a revision, and an event
$post = get_post($post_id);
$slug = sanitize_title($post->post_title);
$newslug = $slug . '-' . tribe_get_start_date( $post_id, false, 'j F Y' );
if ($post->post_name !== $newslug) {
// unhook this function to prevent infinite looping
remove_action( 'save_post', 'change_event_slug_on_save' );
// update the post slug
wp_update_post( array(
'ID' => $post_id,
'post_name' => $newslug
));
// re-hook this function
add_action( 'save_post', 'change_event_slug_on_save', 10, 1 );
}
}
}
add_action( 'save_post', 'change_event_slug_on_save', 10, 1 );
The problem
When I change the event's date, I of course want to update the permalink as well. The trouble is: the event date that I just changed, has not yet been changed in the database, so when updating the permalink on save_post
, it still uses the last stored (old) event date. This means I need to save an event (post) twice to update the link.
I have tried hooking to pre_update_post
, but that gives me the same problem. I have also looked into all the hooks Tribe plug-in offers, but found none that offers to hook into an update event.
Bottom line: I need to find a way to update the permalink when the post editor's fields are updated (before actually publishing or updating a post), using the fields as they are at the time (and not coming from the database). Any help would be very much appreciated.
UPDATE
Thanks to ahendwh2's answer I fixed it, by hooking into Tribe's tribe_events_update_meta
hook, the updated code:
function change_event_slug_on_save( $post_id, $event_data, $event ) {
//$event_data holds the changed event data, $event is the actual event (post)
if ( ! wp_is_post_revision( $post_id ) && tribe_is_event($post_id) ) {
// verify post is not a revision, and an event
$slug = sanitize_title($event->post_title);
$newslug = $slug . '-' . tribe_format_date($event_data['EventStartDate'], false, 'j-F-Y');
if ($event->post_name !== $newslug) {
// unhook this function to prevent infinite looping
remove_action( 'tribe_events_update_meta', 'change_event_slug_on_save' );
// update the post slug
wp_update_post( array(
'ID' => $post_id,
'post_name' => $newslug
));
// re-hook this function
add_action( 'tribe_events_update_meta', 'change_event_slug_on_save', 10, 3 );
}
}
}
add_action( 'tribe_events_update_meta', 'change_event_slug_on_save', 10, 3);