1

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....

  • Why would you expect it to be there? No point in asking questions and giving anly about 10% of the required context to answer them – Mark Kaplun Feb 6 '18 at 6:31
  • @MarkKaplun I've updated my question. Is that info ok to now help? – Sim2K Feb 6 '18 at 8:54
  • What about the first get_post_meta(), that gets the book_id, does it return a ID as integer or anything at all? – Nicolai Feb 6 '18 at 9:08
  • @Nicolai .. it brings back nothing. Creating the CPT Review only drops in this data... the_book_ID = the_requested_reviews = requested_reviews = book_title = Book Review Request the_book_genre_ID = theBookAuthor = 2 And I don't think 'theBookAuthor = 2' is correct as its going off... $theBookAuthor = $post_book_details->post_author; ... which is getting details from ... $post_book_details = get_post( $the_book_ID ); – Sim2K Feb 6 '18 at 9:23
  • 1
    I am not sure you actually save book_id anywhere, at least not in the code here – Mark Kaplun Feb 6 '18 at 9:58
0

I found out that actually, in a new Post creation, the Post Meta is not actually available yet! I was trying to get something that wasn't available!

Check - How to access the post meta of a post that has just been published? .... which says ... "when you publish the post and 'publish_post' is called, the post meta is not yet saved in the database".

Thanks to Nicolai, I used $_Post[FormFeild] to get the required details!

This is my working code, line 6 ($the_book_ID = $_POST['input_2'];) got everything working!

function on_CPT_request_publish( $ID, $post ) {
  // ----------------- Start of Function -----------------
  // -------------------- Grab details to work with -----------------------

  // Get book ID
  $the_book_ID = $_POST['input_2'];

  // Get reviewer count
  $the_requested_reviews = $_POST['input_16']; 
  // Grab the number before the pipe for e.g. 3|69 would = 3
  $vars = explode('|', $the_requested_reviews);
  $the_requested_reviews = $vars[0];  


  // 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;

  // ----------------- Find users -------------------

  // WP_User_Query arguments
  $args = array (
    'number'      => $the_requested_reviews,
    'count_total'     => true,
    'role'          => 'Subscriber',
    'order'         => 'ASC',
    'orderby'       => 'user_registered',
    'meta_query' => array(
      'relation' => 'AND',
      array(
        'key'     => 'user_book_genres',
        'value'   => $the_book_genre_ID,
        'compare' => '='
      ),
      array(
        'key'     => 'user_type',
        'value'   => 'Reviewer',
        'compare' => '='
      ),
      array(
        'key'     => 'user_credits',
        'value'   => '1',
        'compare' => '>='
      )
    )
  );  

  // Create the WP_User_Query object
  $wp_user_query = new WP_User_Query( $args );

  // Get the results
  $ARCusers = $wp_user_query->get_results();

  // Check for results
  if ( ! empty( $ARCusers ) ) {

    // loop through each author
    foreach ( $ARCusers as $ARCuser ) { 

      //Do the magic!

      // get all the user's data
      $ReviewAuthorID = $ARCuser->ID;

      //https://wordpress.stackexchange.com/questions/106973/wp-insert-post-or-similar-for-custom-post-type
      $review_post_id = wp_insert_post(array (
      'post_type' => 'review',
      'post_title' => 'Review of book - ' . $book_title . ' - ' . $ReviewAuthorID,
      'post_author' => $ReviewAuthorID,
      'post_status' => 'publish',
      'comment_status' => 'closed',   // if you prefer
      'ping_status' => 'closed',      // if you prefer
      ));

      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(  'publish_request',  'on_CPT_request_publish', 10, 2 );

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.