0

When our users submit a form (Contact 7), I want to get the next available Auto-Increment number (order-post-ID) and insert this into one of the fields. This is sent along with the form in order to create a link to the Flamingo Inbound Message post.

As of now, it works but is not entirely accurate. Every so often, the number sent is one lower than the actual post ID. For example, one order can be accessed through the dashboard:

/wp-admin/admin.php?page=flamingo_inbound&post=8601

However, the order-post-ID sent with this was 8600. I've tried disabling the buttons and auto-saving to ensure that multiple submissions are not occurring; am I missing something obvious?

My code is below:

function getLastID() {

    global $wpdb;

    $next = $wpdb->get_var($wpdb->prepare(
            "
            SELECT AUTO_INCREMENT
            FROM information_schema.TABLES
            WHERE TABLE_SCHEMA = 'wordpressDB'
            AND TABLE_NAME = 'wp_posts'
            "
            ), 0, 0 );

    echo $next;

    $wpdb->flush();

    exit();
}

Thank you.

2
  • what is in the wp_posts table for ID = 8600 ?
    – mmm
    Commented Sep 19, 2017 at 17:58
  • Thank you, I hadn't even thought to look at the posts which were being lost in the ether - looks like my ajax call is returning data and submitting the post before another function has finished running. Dropped a callback into the code - marking your answer as correct since I believe you've helped to solve this one. Commented Sep 19, 2017 at 19:03

1 Answer 1

0

to have the ID, you can try this hook e.g.

add_action("save_post", function ($post_ID, $post, $update) {


    if (    !$update
        &&  class_exists("Flamingo_Inbound_Message")
        &&  (Flamingo_Inbound_Message::post_type === $post->post_type)
    ) {

        // here $post_ID is the ID of the new created inbound message


    }


}, 10, 3);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.