WordPress.org

Ready to get started?Download WordPress

Forums

Action starting before it's done! (2 posts)

  1. au.petrone
    Member
    Posted 11 months ago #

    Hi people, i'm trying to create a new bbPress forum when new post is saved...I made out this code but the only thing i get it's an infinite loop. The strange is that the loop starts not when i save a post, but first, when i go in "All posts" or in "new Posts". What's the problem please?

    This is the code

    <?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);
    
    }
    
    ?>
  2. au.petrone
    Member
    Posted 11 months ago #

    new code

    <?php
    
    add_action('wp_insert_post', 'register_ForumCustom');
    
    function register_ForumCustom($post_id){
    
    	$post = get_post($post_id);
    
    	$post_status = $post->post_status;
    
    	if($post_status=="auto-draft" || $post_status=="draft"){
    		return;
    	}
    	// 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 "wp_insert_post". I can solve this, but i don't understand the other problem!
    	remove_action('wp_insert_post', 'register_ForumCustom');
    	update_post_meta($post_id, "forum_id", $new_forum_id);
    	add_action('wp_insert_post', 'register_ForumCustom');
    
    }
    
    ?>

Reply

You must log in to post.

About this Topic