Support

Account

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

Solved

differentiate between post types on acf/pre_save_post

  • Dear elliot,

    this should be an easy one, but I just don’t get it.

    If I have several front-end forms creating posts (of different types) and I use acf/pre_save_post filter to manipulate the creation process before the post is saved:

    How can I differentiate between post types in the filter if I can only pass a post_id as parameter?

  • Hi @japel

    A good point,

    I don’t have an answer off the top of my head, but my first idea is to look at the data in the $_POST array. Perhaps there is something in there that you can use to differentiate the forms?

    The acf_form function contains an option to add extra HTML after the fields. You could use this to add in a hidden input containing an ID or something similar.

    Good luck!

    Cheers
    E

  • i can just access the Post array from there?

    $my_var = $_POST[‘my_var’];

    ?

    Best regards,

    Jan

  • Hi @japel

    It all depends on what the input name is, but essentially, yes.

    Good luck

    P.S. – always debug, never assume

  • Hi Elliot,

    I have set 'html_after_fields' => '<input type="hidden" name="post_type" value="products">' in the form to the options array. In the functions.php, I have $post_type = $_POST['post_type']; to reuse the same form for posting different post types.

    But this is breaking and after submission, it redirects to 404. I think this is because the hidden field is nested inside <div id="poststuff">.

    Is there any way I can move it outside the poststuff div?

    I tried looking at the source in api.php but did not find any way to directly set the post type into the $options array. I think it would be great to have an option to set post_type in the options array and if it is not set, then default it to “post”.

    Thanks.

  • Hi @japel

    Input fields should always sit inside a hidden div. This is not the issue.
    Have you debugged your code to find out what is going wrong?

    Thanks
    E

  • You want to debug your code line by line to find out why:
    – this is breaking and after submission

    Perhaps then you can better explain why it is ‘breaking’?

    Thanks
    E

  • I did a var dump of $_POST like so:

    $post_type = $_POST['post_type'];
        echo 'post type: ' . $post_type . '<br/>';
        echo "<pre>";
        var_dump($_POST);
        echo "</pre>";
    
        exit();

    but it seems it never reaches here and throws a 404. The only thing that causes this problem is the hidden field. I have the plugin Debug Objects but am not sure how to catch the error here. A little advice would definitely help me debug and tell you the output.

  • Ok. Here is the debug output:

    array(
    	'acf_nonce' => '665cc6ffdb',
    	'post_id' => 'new',
    	'return' => 'http://localhost:8888/product/submit-product?updated=true',
    	'acf_settings' => '',
    	'post_title' => 'New Product',
    	'post_content' => 'New Product details',
    	'fields' => array(
    		'field_52031e65653cf' => '',
    		'field_518d9f7991091' => '091212',
    		'field_518d9fc591092' => 'product code',
    		'field_51f73964fdb53' => array(
    			'acfcloneindex' => array(
    				'field_51f739d9f6a6f' => '',
    			) ,
    		) ,
    		'field_518da07f91094' => '20131101',
    		'field_518da16d91095' => '',
    		'field_52004906dffcf' => '',
    		'field_52004987277d6' => '',
    		'field_52478ebfe8454' => '110',
    		'field_518da32f9109e' => '',
    		'field_51f729f36ecb8' => '',
    		'field_518da18491096' => '',
    		'field_51f7289e80bf1' => '',
    		'field_51fe885be483c' => '',
    		'field_518da25691098' => '',
    		'field_518da2af9109a' => '',
    		'field_518da2f89109c' => '',
    		'field_51f73aef05ebf' => '',
    		'field_51f895a192c59' => '',
    		'field_51f8968188c4f' => '',
    		'field_518da51759987' => '',
    		'field_51fee9fe9fcb5' => '',
    		'field_51feea629fcb6' => '',
    		'field_51feea779fcb7' => '',
    		'field_520f04f1c58d1' => '',
    	) ,
    	'post_type' => 'products',
    )

    Now I am wondering what’s going wrong. Because it seems the post_type is being set properly. Let me debug some more.

    Thanks.

  • Hi Elliot,

    This is weird. If I set the 'html_after_fields' => '<input type="hidden" name="post_type" value="products"> then it does not invoke the filter acf/pre_save_post. When it is set to empty, it calls the filter properly. Do you have any insights?

    Thanks.

  • Hi @bekar09

    The only thing I can think of is that by adding this argument to the acf_form options, you have some sort of PHP error which is causing the form to not post correctly.

    Can you add in your hidden input code, and then view the page source and search for the word “warning” or ‘error’ or ‘php’?

    Is there any PHP error in the page source?

    Thanks
    E

  • Hi Elliot,

    There is no error in the source except

    <script type='text/javascript'>
    /* <![CDATA[ */
    var pluploadL10n = {"queue_limit_exceeded":"You have attempted to queue too many files.","file_exceeds_size_limit":"%s exceeds the maximum upload size for this site."........

    .

  • Here is my HTML markup:

    <form id="post" class="acf-form" action="" method="post" >
                   <div style="display:none">
                      <script type="text/javascript">
                         acf.o.post_id = "new";
                      </script>
                      <input type="hidden" name="acf_nonce" value="4ceed66667" />
                      <input type="hidden" name="post_id" value="new" />
                      <input type="hidden" name="return" value="http://localhost:8888/product/submit-product?updated=true" />
                      <div id="wp-acf_settings-wrap" class="wp-core-ui wp-editor-wrap tmce-active">
                         <div id="wp-acf_settings-editor-tools" class="wp-editor-tools hide-if-no-js">
                            <a id="acf_settings-html" class="wp-switch-editor switch-html" onclick="switchEditors.switchto(this);">Text</a>
                            <a id="acf_settings-tmce" class="wp-switch-editor switch-tmce" onclick="switchEditors.switchto(this);">Visual</a>
                            <div id="wp-acf_settings-media-buttons" class="wp-media-buttons"><a href="#" id="insert-media-button" class="button insert-media add_media" data-editor="acf_settings" title="Add Media"><span class="wp-media-buttons-icon"></span> Add Media</a></div>
                         </div>
                         <div id="wp-acf_settings-editor-container" class="wp-editor-container"><textarea class="wp-editor-area" rows="20" cols="40" name="acf_settings" id="acf_settings"></textarea></div>
                      </div>
                   </div>
                   <div id="poststuff">
                      <div id="titlediv">
                         <div id="titlewrap"><label class="screen-reader-text" id="title-prompt-text" for="title">Enter title here</label><input type="text" name="post_title" size="30" id="title" placeholder="Enter product title here" autocomplete="off"></div>
                      </div>
                      <div id="wp-frontendform-wrap" class="wp-core-ui wp-editor-wrap html-active">......
                      </div>
                      <div id="acf_75" class="postbox acf_postbox default">......
                      </div>
                      <div><input type="hidden" name="post_type" value="products" /></div>
                      <!-- Submit -->
                      <div class="field">
                         <input type="submit" value="Submit" />
                      </div>
                      <!-- / Submit -->
                   </div>
                   <!-- <div id="poststuff"> -->
                </form>

    The html_after field is <div><input type="hidden" name="post_type" value="products" /></div>

    Thanks.

  • Hi @bekar09

    I wonder if ‘post_type’ is a restricted POST name from WP. Perhaps you could try changing it to ‘my_post_type’ and see if that fixes the issue

    Thanks
    E

  • Hi Elliot,

    Voila! That was a very good catch. I hope this thread will be useful to others looking for similar solution and I guess this will be helpful to you too while integrating Frontend forms for ACF 5.xx.

    Thanks once again.

  • WOW, I had this exact issue a few weeks ago and could not for the life of me figure it out. I was certain it was my own code that was messing it up. However, since I was not able to get ‘post_type’ to work in the html_after_fields parameter I instead used the $_POST[‘post_id’] value and then did a get_post_type(‘$_POST[‘post_id’]) to determine the type within my various functions.

    I think either way works just as well, but my way moves more control to the function rather than relying on the the acf_form array to pass an extra value.

  • 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);
Viewing 17 posts - 1 through 17 (of 17 total)

The topic ‘differentiate between post types on acf/pre_save_post’ is closed to new replies.