Php 使用wp\u Update\u post在保存帖子时更新和添加标签

Php 使用wp\u Update\u post在保存帖子时更新和添加标签,php,wordpress,tags,Php,Wordpress,Tags,我正在尝试构建一个函数,用于保存post wp\u update\u post,以便在代码中添加用逗号分隔的标记 //Create the post array $post = array( 'ID' => 5, 'tags_input' => 'foo,bar,baz'); // Update the post wp_update_post($post); 代码在my function.php主题中运行良好,但是我想启动一个函数,使其仅在文章中运行,或者在使用add_

我正在尝试构建一个函数,用于保存post wp\u update\u post,以便在代码中添加用逗号分隔的标记

//Create the post array
$post = array(
'ID' => 5,
'tags_input' => 'foo,bar,baz');     

// Update the post
wp_update_post($post);
代码在my function.php主题中运行良好,但是我想启动一个函数,使其仅在文章中运行,或者在使用
add_filter(“wp_update_post”),创建新文章的版本中运行
保存日志(“wp\u更新日志”)

我试着这么做

function add_tags($post) {
    global $post;
    $idpost = $post->ID;
    $tags = 'tag1, tag2, tag3, tag4';

    $post = array(
    'ID' => $idpost,
    'tags_input' => $tags);     

    wp_update_post($post);

    return $post;
}
add_filter( 'wp_update_post', 'add_tags');
save_post( 'wp_update_post', 'add_tags');
将和不运行到之间的无限循环中

我会做错什么

已解决:

function my_function(){
    if ( ! wp_is_post_revision( $post_id ) ){

        // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'my_function');

        // update the post, which calls save_post again
        $post = array(
                'ID' => 5,
                'tags_input' => 'foo,bar,baz');  
        wp_update_post( $post );

        // re-hook this function
        add_action('save_post', 'my_function');
    }
}
// hook the function
add_action('save_post', 'my_function');