2012-09-04 1 views
0

Je suis en train de construire une fonction à utiliser pour sauver la wp_update_post post pour ajouter des balises séparées par des virgules comme dans le codejour et ajouter des tags afficher sur Enregistrer après l'aide wp_update_post

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

// Update the post 
wp_update_post($post); 

le code fonctionne très bien dans mon thème function.php, mais je veux commencer une fonction à l'exécution que dans le poste ou dans l'édition la création d'un nouveau poste en utilisant add_filter ('wp_update_post',''); ou save_post('wp_update_post,'');

J'ai essayé de le faire

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

mettre et ne pas courir dans une boucle infinie entre

ce que je peux faire mal

Répondre

0

résolu:

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