admin管理员组文章数量:1122832
I have a plugin with several CPTs (we'll call them CPT-A, CPT-B, CPT-C, and CPT-D). All CPTs have custom fields. In CPT-D, the user is able to make a selection and assign a post in CPT-A, CPT-B, and CPT-C to the current post in CPT-D. Basically linking relevant records to each other.
If the post in CPT-A, CPT-B, and/or CPT-C does not yet exist, the user can "quickly" create the post by entering the post's title using a standard text input field. No other data is required, and any optional data can be entered later.
I then will use wp_insert_post() to add the new post (CPT-A, CPT-B, and/or CPT-C). This all works as expected.
Next, I need to update CPT-D's postmeta with the post ID of the new CPT-A, CPT-B, and/or CPT-C. To do this, I am using update_post_meta().
The problem I'm having is that the wrong post's postmeta is being updated.
For example, if CPT-D's Post ID is 99, it should have postmeta meta_key 'assigned-cpt-a' = 100, 'assigned-cpt-b' = 101, and 'assigned-cpt-c' = 102 if all three CPT's are set.
What happens instead is: CPT-D gets postmeta 'assigned-cpt-a' = 100 CPT-A gets postmeta 'assigned-cpt-b' = 101 CPT-B gets postmeta 'assigned-cpt-c' = 102
Here is a sample of the code that inserts the new CPT and updates postmeta:
<?php
$new_cpta_title = sanitize_text_field( $_POST[ 'cpt-a-name' ] );
/* Only add new if a CPT with the same title doesn't already exist */
if( null == get_page_by_title( $new_cpta_title, OBJECT, 'cpt-a' ) ) {
$new_cpta_slug = preg_replace("/[^A-Za-z0-9]/",'',strtolower($new_cpta_title));
$author_id = get_current_user_id();
$new_cpta = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => $new_cpta_slug,
'post_title' => $new_cpta_title,
'post_status' => 'publish',
'post_type' => 'cpt-a'
);
wp_insert_post( $new_cpta );
$new_cpta_info = get_page_by_path($new_cpta_slug, OBJECT, 'cpt-a');
if ( $new_cpta_info ) {
$new_cpta_ID = $new_cpta_info->ID;
update_post_meta( $post_id, 'cpt-a-id', $new_cpta_ID );
}
}
So, my question is, how can I reset the $post_id in update_post_meta to be the same post ID as the original CPT-D?
I have a plugin with several CPTs (we'll call them CPT-A, CPT-B, CPT-C, and CPT-D). All CPTs have custom fields. In CPT-D, the user is able to make a selection and assign a post in CPT-A, CPT-B, and CPT-C to the current post in CPT-D. Basically linking relevant records to each other.
If the post in CPT-A, CPT-B, and/or CPT-C does not yet exist, the user can "quickly" create the post by entering the post's title using a standard text input field. No other data is required, and any optional data can be entered later.
I then will use wp_insert_post() to add the new post (CPT-A, CPT-B, and/or CPT-C). This all works as expected.
Next, I need to update CPT-D's postmeta with the post ID of the new CPT-A, CPT-B, and/or CPT-C. To do this, I am using update_post_meta().
The problem I'm having is that the wrong post's postmeta is being updated.
For example, if CPT-D's Post ID is 99, it should have postmeta meta_key 'assigned-cpt-a' = 100, 'assigned-cpt-b' = 101, and 'assigned-cpt-c' = 102 if all three CPT's are set.
What happens instead is: CPT-D gets postmeta 'assigned-cpt-a' = 100 CPT-A gets postmeta 'assigned-cpt-b' = 101 CPT-B gets postmeta 'assigned-cpt-c' = 102
Here is a sample of the code that inserts the new CPT and updates postmeta:
<?php
$new_cpta_title = sanitize_text_field( $_POST[ 'cpt-a-name' ] );
/* Only add new if a CPT with the same title doesn't already exist */
if( null == get_page_by_title( $new_cpta_title, OBJECT, 'cpt-a' ) ) {
$new_cpta_slug = preg_replace("/[^A-Za-z0-9]/",'',strtolower($new_cpta_title));
$author_id = get_current_user_id();
$new_cpta = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => $new_cpta_slug,
'post_title' => $new_cpta_title,
'post_status' => 'publish',
'post_type' => 'cpt-a'
);
wp_insert_post( $new_cpta );
$new_cpta_info = get_page_by_path($new_cpta_slug, OBJECT, 'cpt-a');
if ( $new_cpta_info ) {
$new_cpta_ID = $new_cpta_info->ID;
update_post_meta( $post_id, 'cpt-a-id', $new_cpta_ID );
}
}
So, my question is, how can I reset the $post_id in update_post_meta to be the same post ID as the original CPT-D?
Share Improve this question asked Jun 7, 2016 at 21:57 ScottDScottD 1437 bronze badges 2 |1 Answer
Reset to default 0Ultimately the solution that I found was to use global $post; to retrieve the current post's ID.
Immediately after wp_insert_post( $new_cpta );
above, I added
global $post;
$currentid = $post->ID;
Then I was able to update the post meta for the correct post with $currentid
in place of $post_id
.
本文标签: Get original post ID after wpinsertpost
版权声明:本文标题:Get original post ID after wp_insert_post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290403a1928506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$new_cpta_ID = wp_insert_post( $new_cpta );
– czerspalace Commented Jun 7, 2016 at 22:54