我正在使用。。。
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 );
。。。它正在抓取新创建的帖子的详细信息好的,没问题,但当我尝试访问帖子元时,找不到该信息。
任何get\\u post\\u元数据都不起作用,但我已经检查了数据库,并且我在Posteta中查找的记录已经存在。
我还使用了。。。
function on_CPT_request_publish( $ID, $post ) { code stuff }
add_action( \'publish_request\', \'on_CPT_request_publish\', 10, 2 );
但没什么不同。这是我的主要问题。。。
$the_book_ID = get_post_meta($RequestID, \'book_id\', true);
如果我能让这一行正常工作,其他一切都会好起来的(很好,足以让我完成代码调试)。创建的“review”CPT的内容如下。。。
the_book_ID =
book_title = Book Review Request
the_book_genre_ID =
theBookAuthor = 2
当站点前端的表单创建一个名为“Request”的自定义帖子类型时,我们都在关注的这个函数就会启动。“Request”CPT有一个称为“book\\u id”的元值。这个“book\\u id”元值保存了一个名为books的CPT的post id,而这个“book”CPT保存了有关书籍的所有详细信息,如标题、流派、作者等。
现在,当“请求”CPT完成时,它将图书ID保存在一个名为“book\\u ID”的元标记中,完成“请求”CPT也会启动此功能。此函数需要获取“Request”CPT的刚刚发布的表单详细信息,尤其是post meta中的“book\\u id”。我在用这条线$\\u book\\u ID=get\\u post\\u meta($RequestID,\'book\\u ID\',true)。。。要获取要在此处使用的图书ID$post\\u book\\u details=get\\u post(\\u book\\u ID);获取书籍详细信息。
因此,“book\\u id”是一个post元值,它已经存在于我尝试访问的新创建的“Request”CPT中。
希望这现在更有意义。
帮助
SO网友:Sim2K
我发现,实际上,在一个新的帖子创作中,帖子元实际上还不可用!我试图得到一些不可用的东西!
检查-How to access the post meta of a post that has just been published? .... 上面说。。。“发布帖子并调用‘publish\\u post’时,帖子元尚未保存在数据库中”。
多亏了Nicolai$_Post[FormFeild]
获取所需的详细信息!
这是我的工作代码,第6行($the_book_ID = $_POST[\'input_2\'];
) 一切正常!
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 );