问题描述:

I'm trying to build a function to use in saving the post wp_update_post to add tags separated by commas as in the code

//Create the post array

$post = array(

'ID' => 5,

'tags_input' => 'foo,bar,baz');

// Update the post

wp_update_post($post);

the code works fine in my function.php theme, however I want to start a function to be running only in the post or in the edition creating a new post using add_filter ('wp_update_post',''); or save_post('wp_update_post,'');

I tried to do this

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');

put and not run into an infinite loop between

what can I be doing wrong

网友答案:

solved:

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');
相关阅读:
Top