Support

Account

Home Forums General Issues differentiate between post types on acf/pre_save_post Reply To: differentiate between post types on acf/pre_save_post

  • Thanks for the above – quite useful.

    I also use the acf/pre_save_post filter as a place to set the title of my post. This allows me to see the title the list when I click the dashboard ‘All’ link. My code:

    functions.php:

    function my_pre_save_post( $post_id ) {
        // check if this is to be a new post
        if( $post_id != 'new' )
        {
            return $post_id;
        }
        if (isset($_POST['my_pt'])) {
        	// the my_pt is a hidden field added in the parameter array as 'html_after_fields'=>'<div><input type="hidden" name="my_pt" value="'.$my_pt.'">'
        	// this allows us to idenfity which custom post type is being created.
        	switch ($_POST['my_pt']) {
        	case "dvd":
        		$new_post_type="dvd";
        		$title=$_POST['fields']['field_569da15ce7de4'].'('.$_POST['fields']['field_77a4d5aab4545'];
        		$_POST['return'] = $_POST['return'].basename( get_permalink($post_id))."/?editpage=2";
        		break;
        	case "vinyl":
        		$new_post_type="album";
        		$title=$_POST['fields']['field_56a5a9a5640e1'];
        		break;
    		}
    	    // Create a new post
    	    $post = array(
    	        'post_status'  => 'publish',
    	        'post_title'  => $title,
    	        'post_type'  => $new_post_type,
    	    );
    	    // insert the post
    	    $post_id = wp_insert_post( $post );
        }
        // return the new ID
        return $post_id;
    }
    
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );

    and in the template php file I apply to the ‘create new dvd’ page:

    	$acf_form_array=array('post_id' => 'new',
    		'field_groups' => array(5188),
    		'return' => $return_url,
    		'submit_value' => "Save and continue",
    		'html_after_fields' => '<div><input type="hidden" name="my_pt" value="'.$my_pt.'"></div>');
    	acf_form($acf_form_array);