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.
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.
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
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.
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'];