I'm using...
function on_CPT_request_publish( $new, $old, $post ) {
// ----------------- Start of Function -----------------
if ( ( $new == 'publish' ) && ( $old != 'publish' ) && ( $post->post_type == 'request' ) ) {
// -------------------- Grab details to work with -----------------------
$RequestID = $post->ID;
$the_book_ID = get_post_meta($RequestID, 'book_id', true);
// - URL Info - https://developer.wordpress.org/reference/functions/get_post/
// Get Book details
$post_book_details = get_post( $the_book_ID );
$book_title = $post_book_details->post_title;
$the_book_genre_ID = get_post_meta($the_book_ID,'book_genre',true);
$theBookAuthor = $post_book_details->post_author;
$your_content = 'the_book_ID = ' . $the_book_ID . '<br>';
$your_content = $your_content . 'book_title = ' . $book_title . '<br>';
$your_content = $your_content . 'the_book_genre_ID = ' . $the_book_genre_ID . '<br>';
$your_content = $your_content . 'theBookAuthor = ' . $theBookAuthor . '<br>';
$review_post_id = wp_insert_post(array (
'post_type' => 'review',
'post_title' => 'Review of the_book_ID - ' . $post->post_title . ' - Genre - ' . $the_book_genre_ID,
'post_author' => $theBookAuthor,
'post_content' => $your_content,
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
if ($review_post_id) {
// insert post meta
add_post_meta($review_post_id, 'review_book_id', $the_book_ID);
add_post_meta($review_post_id, 'book_author_id', $theBookAuthor);
}
// ----------------- End of Function -----------------
}
}
add_action( 'transition_post_status', 'on_CPT_request_publish', 201, 3 );
... it's grabbing the post details of the newly created post OK, no problem, but when I try and access the post meta, that info can't be found.
Any get_post_meta stuff is not working but I've checked the DB and the record I'm looking for in Postmeta exists.
I have also used ...
function on_CPT_request_publish( $ID, $post ) { code stuff }
add_action( 'publish_request', 'on_CPT_request_publish', 10, 2 );
But made no difference. This is my main issue ...
$the_book_ID = get_post_meta($RequestID, 'book_id', true);
If I can get this line to work, everything else will be ok (well OK enough for me to finish debugging my code). The content of the created 'review' CPT is this ...
the_book_ID =
book_title = Book Review Request
the_book_genre_ID =
theBookAuthor = 2
This function that we are all looking at kicks off when a form on the front end of the site creates a custom post type called 'Request'. The 'Request' CPT has a meta value called 'book_id'. This 'book_id' meta value holds the post ID of a CPT called books and that 'Book' CPT holds all the details about books like, title, genre, Author, etc.
Now when a 'Request' CPT is completed, it holds the book ID in a meta tag called 'book_id', and the completion of the 'Request' CPT also kicks off this function. This function needs to grab the just enetred form details for the 'Request' CPT, especially the 'book_id' in the post meta. I'm using this line ..... $the_book_ID = get_post_meta($RequestID, 'book_id', true); ... to get that book ID to use here ... $post_book_details = get_post( $the_book_ID ); to get the book details.
So 'book_id' is a post meta value that already exists in the newly created 'Request' CPT that I am try to access.
Hope this makes a bit more sense now.
Help....
get_post_meta()
, that gets the book_id, does it return a ID as integer or anything at all?