• Resolved AnotherChance

    (@anotherchance)


    Good afternoon, I have created a front-end form for logged in users to submit posts. Once the post is created they are redirected to a confirmation page, which is also on the front end, and this is where i have an issue. I want the newly created $post_id to be carried to this page where it will be inserted into a custom table along with other data. I have created a redirect function in my functions.php file.

    function myFunction() {
      if ( wp_insert_post == true) {
       $postid = get_the_ID();
       wp_redirect (home_url( '/custom_page/' ));
      }
     }
     add_action( 'save_post', 'myFunction' );

    The redirect works but I just can’t populate my $postid. I have tried to echo it but it comes with nothing.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Get_the_id() can only be used when you are in the ‘loop’ on the front end.

    Instead ‘save_post’ passes the post_id with the action. Check the documentation on that.

    Thread Starter AnotherChance

    (@anotherchance)

    If I understand you correctly, I can’t use get_the_ID() for this specific action but I should be able to use $post_id as it passes with save_post automatically. I have tried calling $post_id on the confirmation page but it comes up blank. Is there any specific way i should structure the call and where do i put it?

    Like this:

    function myFunction($post_id) {

    The post_id is passed with the action. So it will only be available on inside the function linked to that action. If you want it somewhere else you’ll have to pass it on.

    Also I don’t know what this code is :

    wp_insert_post == true

    but I can imagine that doesn’t work either. I haven’t created posts on the front before so I don’t know how it should be.

    Thread Starter AnotherChance

    (@anotherchance)

    Right, after messing around and trying to understand the documentation.Some of the data on the confirmation page has been inserted into table, less the $post_id. This is progress though as it didn’t do anything before. So what I did was the following code:

    global $wpdb;
     $table = 'my table';
     $update_info = array(
      'post_id' => get_post( $post_id ),
      'post_days' => $_POST['total_days'],
      'status' => 'pending'
     );
     array('%s', '%s', '%s');
     if(isset($_POST['update'])) {
     $wpdb -> insert($table, $update_info);
     };

    I am now getting an error: Warning: mysqli_real_escape_string() expects parameter 2 to be string, object given.
    C:\xampp\htdocs\wordpress\wp-includes\wp-db.php on line 1127

    Thread Starter AnotherChance

    (@anotherchance)

    wp_insert_post() is a wordpress function that I use when inserting data into wp_posts table from custom forms. it does work as it redirects to the desired page once the post is created.

    Thread Starter AnotherChance

    (@anotherchance)

    I finally got this resolved. The following code returned the $post_id of the last post for the logged in user.

    $last = wp_get_resent_posts(array( 'numberposts' => '1', 'post_type' => 'post', 'post_status' => 'pending', 'author' => $user_id));
    $REFERENCE = $LAST['0']['ID'];

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘$post_id’ is closed to new replies.