Support

Account

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

  • Thanks James! It’s really weird, I’m starting to wonder if the functions.php code is firing at all. I’ve made changes and thought it looked good, but still no luck. All I get is a completely blank post that I can’t delete from the dashboard. I did update the code to the following…

    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',
    					'field_groups' => array(4)
    				));
    			
    				
    				 ?>

    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_post' ) {
      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, 'news_post_tags', $customMetaField, true);
    	add_post_meta($post_id, 'news_post_content', $customContentField, true);
        add_post_meta($post_id, 'news_post_title', $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);