0

我正在尝试在保存新帖子时创建一个新的 bbPress 论坛......我制作了这段代码,但我唯一得到的是一个无限循环。奇怪的是,循环不是在我保存帖子时开始,而是首先在我进入“所有帖子”或“新帖子”时开始。请问有什么问题吗?

这是代码

<?php

add_action('save_post', 'register_ForumCustom');

function register_ForumCustom($post_id){

    $post = get_post($post_id);
    // Create post object
    $my_new_post = array(
      'post_title'    => 'Forum di'.$post->post_title,
      'post_content'  => '',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type' => 'forum'
    );

    // Insert the post into the database
    $new_forum_id = wp_insert_post( $my_new_post );

    //OK, maybe is here that the loop starts, but i know that the problem is because there is another "save_post". I can solve this, but i don't understand the other problem!

    update_post_meta($post_id, "forum_id", $new_forum_id);

}

?>
4

1 回答 1

0

你用你的动作创建了无限循环。

你必须解开你的动作并在你完成后重新钩住它:

// unhook
remove_action('save_post', 'register_ForumCustom');

// do updates/inserts

// re-hook
add_action('save_post', 'register_ForumCustom');

见:http ://codex.wordpress.org/Plugin_API/Action_Reference/save_post#Avoiding_infinite_loops

于 2013-05-18T21:50:33.557 回答