Tagged: ,

Viewing 6 reply threads
  • Author
    Posts
    • #6415
      mcmaster
      Member

      I have a front-end form that creates a post. The post is created just fine and I can see the input data in $_POST, but how do I access the ID of the newly-created post so I can do something with it?

      I’m using 0.9.9.8. In 0.9.5v, the new ID showed up in a hidden ID field, but now that field is empty:

      piklist('field', array(
      	'type' => 'hidden',
      	'field' => 'ID',
      	'scope' => 'post',
      	'required' => false,
      ));
      

      I tried the new-post-with-validation.php form in piklist-demos, but its $_POST results don’t include an ID field.

      Thanks for any clues as I’ve been banging my head on this for an hour!

      Donna

    • #6419
      Jason
      Keymaster

      Hi Donna!

      That’s no good. That’s obviously important.

      I’m looking into this. Can you please verify that if you look in $_GET instead of $_POST you’re not seeing the ID in there, either? The $_POST will contain the results of the form, but it looks like the fields are also being stored in the query var of the redirect url.

      Thanks!

    • #6420
      mcmaster
      Member

      Jason, thanks so much! The ID is not in either. I started looking at $_POST and then switched to $_REQUEST to make sure I wasn’t missing anything in $_GET. But nothing is coming via $_GET.

      I started out using a redirect field, but I was receiving nothing at the target, so then I removed the redirect and just put a piklist::pre( $_REQUEST ) in the beginning of the form page. Here’s what I get:

      Array
      (
          [_post] => Array
              (
                  [post_type] => ticket
                  [post_status] => draft
                  [post_title] => Donna Test
                  [ID] => 
              )
      
          [_post_meta] => Array
              (
                  [phone] => 541-738-2973
                  [email] => [email protected]
                  [tickets_season] => Array
                      (
                          [0] => 2
                      )
      
                  [tickets_c1878] => Array
                      (
                          [0] => 0
                      )
      
                  [tickets_c1897] => Array
                      (
                          [0] => 0
                      )
      
                  [tickets_c1899] => Array
                      (
                          [0] => 0
                      )
      
                  [tickets_c1931] => Array
                      (
                          [0] => 1
                      )
      
                  [donation] => 
                  [address_1] => 123 X
                  [address_2] => 
                  [city_state] => Portland OR
                  [total_due] => 130
                  [total_paid] => 0
              )
      
          [submit] => Review Order
          [_] => Array
              (
                  [form_id] => theme_ticket_order_form
                  [nonce] => 19175e9a65
                  [fields] => 6615d42e5a870dacde95e626deea7ee2
              )
      
      )

      That’s when I have the hidden ID field in the form; if I remove that field I have no ID column in the _post array.

      Donna

    • #6421
      mcmaster
      Member

      I just simplified the form as it had a lot of code used to figure out what the ticketable events are:

      <?php
      /*  
      Title: Test Order Form
      Method: post
      Message: Data saved as a post of type ticket.
      */
      
      // Set where to save this form
      piklist('field', array(
      	'type' => 'hidden',
      	'scope' => 'post',
      	'field' => 'post_type',
      	'value' => 'ticket',
      ));
      
      piklist('field', array(
      	'type' => 'hidden',
      	'scope' => 'post',
      	'field' => 'post_status',
      	'value' => 'draft',
      ));
      
      // Collect information
      piklist('field', array(
      	'type' => 'text',
      	'scope' => 'post', // post_title is in the wp_posts table
      	'field' => 'post_title',
      	'label' => 'Name',
      	'columns' => 10,
      	'required' => true
      ));
      
      piklist('field', array(
      	'type' => 'text',
      	'scope' => 'post_meta',
      	'field' => 'phone',
      	'label' => 'Phone',
      	'columns' => 10,
      	'required' => true,
      ));
      
      // New post ID
      piklist('field', array(
      	'type' => 'hidden',
      	'field' => 'ID',
      	'scope' => 'post',
      	'required' => false,
      ));
      
      // Submit button
      piklist('field', array(
      	'type' => 'submit',
      	'field' => 'submit',
      	'value' => 'Review Order',
      	'attributes' => array(
      		'class' => 'btn btn-default',
      	),
      ));
      
    • #6422
      mcmaster
      Member

      Posted $_RESULT for the simplified form:

      Array
      (
          [_post] => Array
              (
                  [post_type] => ticket
                  [post_status] => draft
                  [post_title] => Donna Test
                  [ID] => 
              )
      
          [_post_meta] => Array
              (
                  [phone] => 555-555-1212
              )
      
          [submit] => Review Order
          [_] => Array
              (
                  [form_id] => theme_ticket_order_form
                  [nonce] => 409df09d78
                  [fields] => 77fa371f86f4a61bb87df398d2260f27
              )
      
      )
      
    • #6424
      mcmaster
      Member

      Kevin explained that because a form can now create multiple objects, the ID field doesn’t work any more. He suggested using a couple of hooks to grab the ID and stick it into the redirect URL. Here are the functions that did the job. (If you’re not doing a redirect, you’ll just need the first one.)

      add_action( 'piklist_save_field-post', 'my_grab_new_post_id' );
      function my_grab_new_post_id ( $fields ) {
      	global $mcw_new_post_id;
      	$my_new_post_id = $fields['ID']['object_id'];
      }
      
      add_filter( 'wp_redirect', 'my_stick_new_post_id_in_redirect', 10, 2 );
      function my_stick_new_post_id_in_redirect ( $location, $status ) {
      	global $my_new_post_id;
      	return( $location."?ID=$my_new_post_id" );
      }
      
      • #11378
        xolotl
        Member

        Greetings fellow Piklisters! Perhaps I’m being dense, but I’m still not seeing how to redirect a user to a new post they’ve made via a Piklist front-end form.

        I see @sbruner has suggested the Redirect parameter in the form comment block, but while I can see Redirects to static URLs working, I don’t see any hints on how to use the Redirect parameter to redirect to the new post created by a front-end form.

        I’ve also tried @mcmaster’s solution above, but have not been able to use it to generate the actual ID of the new post. Though this doesn’t solve the issue, I wonder if there are some typos in that code.

        Shouldn’t global $mcw_new_post_id; be global $my_new_post_id;?

        Shouldn’t return( $location."?ID=$my_new_post_id" ); be return( $location."?ID=".$my_new_post_id" );?

    • #6429
      Jason
      Keymaster

      Glad Kevin was able to clear it up. Thanks for posting the solution here!

Viewing 6 reply threads
  • You must be logged in to reply to this topic.