Support

Account

Home Forums Feature Requests Pre-populate a new post from recent post objects

Helping

Pre-populate a new post from recent post objects

  • /*
    * Function will copy the data from a selected post object to a new post
    */
    function copy_post_object ($post, $post_id)
    {
        // bail early if no ACF data
        if( empty($_POST['acf']) ) {
            
            return;
            
        }
        
    	$curr_post = get_post();
    	$post_id = $curr_post->ID;
    	$my_post = array();
    	$my_post['ID'] = $post_id;
    	
    	$po_id = get_post_meta($post_id, 'class', true);
    	
    	if($po_id !== NULL) /*Post_object to link to exists (User selected a post_object to copy*/
    	{
    		$po_instructor = get_post_meta($po_id,'instructor',true);
    		$po_class = get_post_meta($po_id,'class',true);
    		$po_provider = get_the_category($po_id);
    		
    		$my_post['post_title'] = get_the_title($po_class);
    
    		update_post_meta($post_id, 'instructor', $po_instructor);
    		wp_set_post_categories($post_id, $po_provider[0]);
    		wp_update_post($my_post);
    	}
    
    }
    add_action('acf/save_post', 'copy_post_object', 10);
    

    I’m trying to allow the user to pre populate the title (class name) , provider(category) and instructor which is a ACF (text area). Has anyone had any experience. The pre population is done through a post object of all the post titles. This is specifically that the user does not have to fill in a title and can select a past post object fill in who the instructor was and write their review and be done.

    EDIT: I’ve been following this but this is more for updating than pre populating on a new post
    Link

  • Have you thought about using a plugin that allows posts to be duplicated, like https://wordpress.org/plugins/duplicate-post/

    If you want to do it yourself you’ll also need to insert the field key for the ACF fields, for an explanation of these fields take a look at this page http://www.advancedcustomfields.com/resources/update_field/

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Pre-populate a new post from recent post objects’ is closed to new replies.