Short: 
我想在保存后更改帖子的slug(用于永久链接),并使用我刚刚在帖子编辑器中输入的数据对其进行修改。
Long:
我当前正在使用
Tribe Events Calendar 插件,并希望创建具有相同标题的多个事件,因此为了确保永久链接是唯一的,我在保存事件(post)时将事件日期(与post一起存储)添加到slug的末尾。
What I have done so far
我使用
save_post 挂钩:
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
当我更改活动的日期时,我当然也想更新永久链接。问题是:我刚刚更改的事件日期在数据库中还没有更改,所以在
save_post, 它仍然使用
last stored (旧)事件日期。这意味着我需要保存一个事件(post)两次来更新链接。
我试着pre_update_post, 但这给了我同样的问题。我还查看了所有的hooks-Tribe插件,但没有发现任何提供挂钩到更新事件的插件。
Bottom line: 我需要找到一种方法,在更新帖子编辑器的字段时(在实际发布或更新帖子之前),使用当时的字段(而不是来自数据库)更新永久链接。任何帮助都将不胜感激。
<小时>
UPDATE
幸亏
ahendwh2\'我的答案是,我通过勾住部落的
tribe_events_update_meta 钩子,更新代码:
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);
 
                    最合适的回答,由SO网友:ahendwh2 整理而成
                    尝试使用操作提供的post对象作为第二个参数,而不是从数据库获取它:
function change_event_slug_on_save( $post_id, $post ) {
    ...
}
add_action( \'save_post\', \'change_event_slug_on_save\', 10, 2 );
 这样,您就可以删除该行
$post = get_post($post_id);<小时>EDIT:部落事件日历有自己的保存事件的操作:tribe_events_event_save 和tribe_events_update_meta. 由于事件日期保存在post\\u meta中,因此应使用第二个操作:
/**
 * @param int     $event_id The event ID (alias post ID).
 * @param array   $data     The saved meta fields. The new date should be somewhere in this array
 * @param WP_Post $event    The event itself (alias $post).
 */
function change_event_slug_on_save($event_id, $data, $event) {
    ...
}
add_action(\'tribe_events_update_meta\', \'change_event_slug_on_save\', 10, 3);