Support

Account

Home Forums Backend Issues (wp-admin) Create Multiple Posts on Back End Reply To: Create Multiple Posts on Back End

  • I’ve given this a shot (just testing it on the front end before I figure out how to add it on the back end) and I can create a single, untitled post with no content ๐Ÿ™‚

    Here’s what I’ve got in the front end template:

    <?php
    		
    		// Bail if not logged in or able to post
        if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) {
            echo '<p>You must be a registered author to post.</p>';
            return;
        }
    		
    		 acf_form(array(
    					'post_id'		=> 'new_post',
    					'form'               => true,
    					'field_groups' => array(4),
    					'new_post'		=> array(
    						'post_type'		=> 'post',
    						'post_status'		=> 'publish'
    					),
    					'submit_value'		=> 'Save Posts',
    					'updated_message' => __("Post updated", 'acf')
    				));
    				
    				 ?>

    And in functions.php…

    // Special ACF Function for Creating Multiple Daily News Brief Posts
    // This function imports multiple posts at once
    function acf_import_multiple_posts( $post_id ) {
      // If the post_id is not equal to 'new', skip the entire function
      if( $post_id != 'new' ) {
        return $post_id;
      };
    
      $loop = $_POST['acf']['field_57614ac1e5179']; //ACF Repeater Field key
      $customMetaField = 'news_post_tags'; // Custom Meta Field
      $customContentField = 'news_post_content'; // Custom Content Field
      $customPostTitle = 'news_post_title'; // Custom Title
    
      $count = count($loop); // Get the number of rows in the Repeater Field
    
      for ($key = 0; $key < $count; ++$key) { // If $count is 5, run for loop till it reaches 5 times
        // Swap out $key for each row number
        $customMetaField = trim($_POST['acf']['field_5762b0a5ad19f'][$key]['field_5762b0a5ad19f']);
        $customContentField = trim($_POST['acf']['field_57614bf6c6b13'][$key]['field_57614bf6c6b13']);
        $customPostTitle = trim($_POST['acf']['field_57614baac6b12'][$key]['field_57614baac6b12']);
    
        // Create a new App Post from posts listed in the repeater field
        $post = array(
          'post_status'  => 'publish', // Set to draft because the user still needs to review
          'post_title' => $customPostTitle, // Actual Post Title
          'post_name' => sanitize_title( $customPostTitle ), // Actual Post Slug
    	  'post_content' => $customContentField, // Post Content
          'post_type'  => โ€˜postโ€™ // Post Type
        );
    
        $post_id = wp_insert_post( $post );     // Use the WordPress default wp_insert_post function
    
        // Set the Custom Meta Fields with the $customMetaField, $customContentField and $customPostTitle
        add_post_meta($post_id, 'customMetaField', $customMetaField, true);
    	add_post_meta($post_id, 'customContentField', $customContentField, true);
        add_post_meta($post_id, 'customPostTitle', $customPostTitle, true);
      }
    
      // update $_POST['return']
      $_POST['return'] = add_query_arg( array('post_id' => $post_id), $GLOBALS['acf_form']['return'] );
    
      // return the new ID
      return $post_id;
    }
    add_action('acf/pre_save_post', 'acf_import_multiple_posts', 20);

    I know I’m doing something very wrong, but I’m just not sure what. Anything in particular stand out? Thanks so much for your help!