0

I can't get the published post ID, i'm using a front end custom form. When the submit button is clicked site is re directed to a different page. Now, i can't get the just published post ID using:

wp_insert_post($post)

Because it returns the ID of redirected page instead of the published post. How can i get it? I'm running in circles here.

  • I'm am unsure what you mean. wp_insert_post() should return the actual created post. Can you explain further? – Howdy_McGee Jan 26 '16 at 1:24
1

wp_insert_post() returns either one of three things

  • the newly created post's ID

  • a WP_Error object if $wp_error is set to true if an error occured during post insertion in which case the post is not inserted

  • 0 if $wp_error is set to false (default) if an error occured during post insertion in which case the post is not inserted

The following should work

$args = [
    // All your post arguments
];
$q = wp_insert_post( $q );
if (    0 != $q 
     && !is_wp_error( $q )
) {
    echo 'My new post ID is ' . $q;
}
  • Hi, yes this works. But my post is generated from a function in the functions file. I'm developing a three steps reservations process and the form takes the user through each step, form 1 takes user to form 2 and that one takes the user to form 3. The form uses the function and when redirections happens the $q returns the ID of the actual page. I get help from other site, the correcto way to achieve this is to declare this in the redirected page (in my case is a template): if( $_POST) { $p = name_of_my_function(); } – Al Rosado Jan 26 '16 at 15:44
  • You should make sure that you post all relevant stuff in your question. I have answered what was given in your question, I could never know the extra details you posted. Please be sure that you post all relevant info next time so you can get specific answers to your exact problem – Pieter Goosen Jan 26 '16 at 16:09
0

I am using a custom_function() to insert the post, the form redirects the user to a different page, that page has it's own ID and that's why I couldn't get this to work.

In my case worked declaring this in my template:

if( $_POST) {
    $post_ID = custom_function();
}

Then i can use $post_ID to get the custom data I needed from that post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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