Ask your WordPress questions! Pay money and get answers fast! (more info)

Get Post ID after frontend post submit and page redirection WordPress

  • SOLVED

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.

Answers (3)

2016-01-26

Romel Apuya answers:

What is your current code for saving the post?


Alvaro Rosado comments:

This is in my functions.php file

function reservation_saving_post() {

if ( empty($_POST) || !wp_verify_nonce($_POST['reserve_meta_noncename'],'save_post_action') )
{
print 'Sorry, your nonce did not verify.';
exit;

}else{

if (isset ($_POST['_reserveTitle'])) {
$reserveTitle = $_POST['_reserveTitle'];
} else {
echo '';
exit;
}
if (isset ($_POST['_reserveDescription'])) {
$reserveDescription = $_POST['_reserveDescription'];
} else {
echo 'Por favor escriba sus comentarios';
exit;
}
if (isset ($_POST['_reserveName'])) {
$reserveName = $_POST['_reserveName'];
} else {
echo 'Por favor escriba su nombre';
exit;
}
if (isset ($_POST['_reserveLastname'])) {
$reserveLastname = $_POST['_reserveLastname'];
} else {
echo 'Por favor escriba su apellido';
exit;
}

if (isset ($_POST['_reserveMembersCode'])) {
$reserveMembersCode = $_POST['_reserveMembersCode'];
} else {
}

if (isset ($_POST['_reserveEmail'])) {
$reserveEmail = $_POST['_reserveEmail'];
} else {
echo 'Por favor escriba su email';
exit;
}

if (isset ($_POST['_reserveTel'])) {
$reserveTel = $_POST['_reserveTel'];
} else {
echo 'Por favor escriba su telefono';
exit;
}

if (isset ($_POST['_reserveMobile'])) {
$reserveMobile = $_POST['_reserveMobile'];
} else {
}

if (isset ($_POST['_reserveFacebook'])) {
$reserveFacebook = $_POST['_reserveFacebook'];
} else {
}

if (isset ($_POST['_reserveCountry'])) {
$reserveCountry = $_POST['_reserveCountry'];
} else {
echo 'Por favor escriba su país';
exit;
}

if (isset ($_POST['_reserveComments'])) {
$reserveComments = $_POST['_reserveComments'];
} else {
}

if (isset ($_POST['_reserveArrivalDate'])) {
$reserveArrivalDate = $_POST['_reserveArrivalDate'];
} else {
echo 'Por favor escriba su fecha de llegada';
exit;
}

if (isset ($_POST['_reserveArrivalTime'])) {
$reserveArrivalTime = $_POST['_reserveArrivalTime'];
} else {
echo 'Por favor escriba su hora de llegada';
exit;
}

if (isset ($_POST['_reserveArrivalAirline'])) {
$reserveArrivalAirline = $_POST['_reserveArrivalAirline'];
} else {
echo 'Por favor escriba su aerolinea de llegada';
exit;
}

if (isset ($_POST['_reserveArrivalFlightNumber'])) {
$reserveArrivalFlightNumber = $_POST['_reserveArrivalFlightNumber'];
} else {
echo 'Por favor escriba el número de vuelo de llegada';
exit;
}

if (isset ($_POST['_reserveShuttleType'])) {
$reserveShuttleType = $_POST['_reserveShuttleType'];
} else {
echo 'Por favor escriba el tipo de traslado';
exit;
}

if(isset($_POST['post_tags'])){
$tags = $_POST['post_tags'];
}else{
$tags = "";
}

$post = array(
'post_title' => wp_strip_all_tags( $reserveTitle ),
'post_content' => $reserveDescription,
//'post_category' => $_POST['cat'],
//'tags_input' => $tags,
'post_status' => 'publish',
'post_type' => $_POST['post-type']
);
$pid = wp_insert_post($post); // http://codex.wordpress.org/Function_Reference/wp_insert_post
add_post_meta($pid, '_reserveName', $reserveName, true);
add_post_meta($pid, '_reserveLastname', $reserveLastname, true);
add_post_meta($pid, '_reserveMembersCode', $reserveMembersCode, true);
add_post_meta($pid, '_reserveEmail', $reserveEmail, true);
add_post_meta($pid, '_reserveTel', $reserveTel, true);
add_post_meta($pid, '_reserveMobile', $reserveMobile, true);
add_post_meta($pid, '_reserveFacebook', $reserveFacebook, true);
add_post_meta($pid, '_reserveCountry', $reserveCountry, true);
add_post_meta($pid, '_reserveComments', $reserveComments, true);
add_post_meta($pid, '_reserveArrivalDate', $reserveArrivalDate, true);
add_post_meta($pid, '_reserveArrivalTime', $reserveArrivalTime, true);
add_post_meta($pid, '_reserveArrivalAirline', $reserveArrivalAirline, true);
add_post_meta($pid, '_reserveArrivalFlightNumber', $reserveArrivalFlightNumber, true);
add_post_meta($pid, '_reserveShuttleType', $reserveShuttleType, true);

$increment = get_option('reservation_increment') +1;

if ( ! add_post_meta( $pid, 'reservation', $increment, true ) ) {
update_post_meta ( $pid, 'reservation', $increment );
}

update_option('reservation_increment',$increment);

return $pid;


}

}


This is my form:


<!-- redirection happens in the form -->
<form id="save-reserve-post-type" class="form form-validated has-margin-bottom-inputs" name="save-reserve-post-type" method="post" action="<?php echo bloginfo('url') . '/reservaciones-paso-tres/' ?>" class="form-group-lg">
<!-- all my inputs here -->
</form>


This is is redirected page by the form:

if( $_POST) {
//this is where i tried to recover the id by using $pid
reservation_saving_post();
}


Alvaro Rosado comments:

This is in my functions.php file


function reservation_saving_post() {



if ( empty($_POST) || !wp_verify_nonce($_POST['reserve_meta_noncename'],'save_post_action') )

{

print 'Sorry, your nonce did not verify.';

exit;



}else{



if (isset ($_POST['_reserveTitle'])) {

$reserveTitle = $_POST['_reserveTitle'];

} else {

echo '';

exit;

}

if (isset ($_POST['_reserveDescription'])) {

$reserveDescription = $_POST['_reserveDescription'];

} else {

echo 'Por favor escriba sus comentarios';

exit;

}

if (isset ($_POST['_reserveName'])) {

$reserveName = $_POST['_reserveName'];

} else {

echo 'Por favor escriba su nombre';

exit;

}

if (isset ($_POST['_reserveLastname'])) {

$reserveLastname = $_POST['_reserveLastname'];

} else {

echo 'Por favor escriba su apellido';

exit;

}



if (isset ($_POST['_reserveMembersCode'])) {

$reserveMembersCode = $_POST['_reserveMembersCode'];

} else {

}



if (isset ($_POST['_reserveEmail'])) {

$reserveEmail = $_POST['_reserveEmail'];

} else {

echo 'Por favor escriba su email';

exit;

}



if (isset ($_POST['_reserveTel'])) {

$reserveTel = $_POST['_reserveTel'];

} else {

echo 'Por favor escriba su telefono';

exit;

}



if (isset ($_POST['_reserveMobile'])) {

$reserveMobile = $_POST['_reserveMobile'];

} else {

}



if (isset ($_POST['_reserveFacebook'])) {

$reserveFacebook = $_POST['_reserveFacebook'];

} else {

}



if (isset ($_POST['_reserveCountry'])) {

$reserveCountry = $_POST['_reserveCountry'];

} else {

echo 'Por favor escriba su país';

exit;

}



if (isset ($_POST['_reserveComments'])) {

$reserveComments = $_POST['_reserveComments'];

} else {

}



if (isset ($_POST['_reserveArrivalDate'])) {

$reserveArrivalDate = $_POST['_reserveArrivalDate'];

} else {

echo 'Por favor escriba su fecha de llegada';

exit;

}



if (isset ($_POST['_reserveArrivalTime'])) {

$reserveArrivalTime = $_POST['_reserveArrivalTime'];

} else {

echo 'Por favor escriba su hora de llegada';

exit;

}



if (isset ($_POST['_reserveArrivalAirline'])) {

$reserveArrivalAirline = $_POST['_reserveArrivalAirline'];

} else {

echo 'Por favor escriba su aerolinea de llegada';

exit;

}



if (isset ($_POST['_reserveArrivalFlightNumber'])) {

$reserveArrivalFlightNumber = $_POST['_reserveArrivalFlightNumber'];

} else {

echo 'Por favor escriba el número de vuelo de llegada';

exit;

}



if (isset ($_POST['_reserveShuttleType'])) {

$reserveShuttleType = $_POST['_reserveShuttleType'];

} else {

echo 'Por favor escriba el tipo de traslado';

exit;

}



if(isset($_POST['post_tags'])){

$tags = $_POST['post_tags'];

}else{

$tags = "";

}



$post = array(

'post_title' => wp_strip_all_tags( $reserveTitle ),

'post_content' => $reserveDescription,

//'post_category' => $_POST['cat'],

//'tags_input' => $tags,

'post_status' => 'publish',

'post_type' => $_POST['post-type']

);

$pid = wp_insert_post($post); // http://codex.wordpress.org/Function_Reference/wp_insert_post

add_post_meta($pid, '_reserveName', $reserveName, true);

add_post_meta($pid, '_reserveLastname', $reserveLastname, true);

add_post_meta($pid, '_reserveMembersCode', $reserveMembersCode, true);

add_post_meta($pid, '_reserveEmail', $reserveEmail, true);

add_post_meta($pid, '_reserveTel', $reserveTel, true);

add_post_meta($pid, '_reserveMobile', $reserveMobile, true);

add_post_meta($pid, '_reserveFacebook', $reserveFacebook, true);

add_post_meta($pid, '_reserveCountry', $reserveCountry, true);

add_post_meta($pid, '_reserveComments', $reserveComments, true);

add_post_meta($pid, '_reserveArrivalDate', $reserveArrivalDate, true);

add_post_meta($pid, '_reserveArrivalTime', $reserveArrivalTime, true);

add_post_meta($pid, '_reserveArrivalAirline', $reserveArrivalAirline, true);

add_post_meta($pid, '_reserveArrivalFlightNumber', $reserveArrivalFlightNumber, true);

add_post_meta($pid, '_reserveShuttleType', $reserveShuttleType, true);



$increment = get_option('reservation_increment') +1;



if ( ! add_post_meta( $pid, 'reservation', $increment, true ) ) {

update_post_meta ( $pid, 'reservation', $increment );

}



update_option('reservation_increment',$increment);



return $pid;





}



}




This is my form:

<!-- redirection happens in the form -->

<form id="save-reserve-post-type" class="form form-validated has-margin-bottom-inputs" name="save-reserve-post-type" method="post" action="<?php echo bloginfo('url') . '/reservaciones-paso-tres/' ?>" class="form-group-lg">

<!-- all my inputs here -->

</form>




This is is redirected page by the form:


if( $_POST) {

//this is where i tried to recover the id by using $pid

reservation_saving_post();

}


Romel Apuya comments:

why not do the redirect inside the function reservation_saving_post()

replace return

return $pid;

with

if( !empty($pid)) {
$permalink = get_permalink($pid);
wp_redirect($permalink);
exit;

} else {

}


but if you want to use the function reservation_saving_post() without changes on the page where

if( $_POST) {
//this is where i tried to recover the id by using $pid
reservation_saving_post();
}


replace with

if( !empty(reservation_saving_post())) {
$permalink = get_permalink(reservation_saving_post());
wp_redirect($permalink);
exit;

} else {

}


Alvaro Rosado comments:

I'm building a three steps reservation, the form takes the user to the last step, besides the reservation won't have it's own permalink cause it will be hidden from regular users and available to admins only.

Don't know if I'm doing it wrong but it's working so far, I only need to get one data from the published post but I can't without the id.


Romel Apuya comments:

ok..

try this

if( $_POST) {
//this is where i tried to recover the id by using $pid
$pid = reservation_saving_post();
if( !empty($pid)) {
$permalink = get_permalink($pid);
wp_redirect($permalink);
exit;
} else {

}
}


$pid = reservation_saving_post();
$pid will return the id of the post since thats the return value of the function


Alvaro Rosado comments:

Bro you just saved my day!!!

2016-01-26

Kyle answers:

Did you write your form? It would help if we can view the full code you are using

2016-01-26

Luis Abarca answers:

Paste your code to get a better idea.

You can try to catch the ID and then send it as an argument in the URL.


$post_id = wp_insert_post($your_post_data);

if ( is_wp_error($post_id) ) {
// Error, post not inserted
} else {
$url = add_query_arg( 'post_id', $post_id, 'http://yourdestinationurl.com' );
wp_redirect($url);
die;
}