admin管理员组文章数量:1130349
I'working on plugin for custom post types and it's metadata etc. Everything is fine so far but now I need to extend this plugin - I need to create one extra the same custom post on save/edit post when certain metadata has value X. And I'm stucked to figure out how to get 2 values: 1)Insert "Parent" post ID to "Child" post to specific metadata 2)Insert "Child" post ID to "Parent" post to specific metadata
Here is code:
add_action('save_post', array($this, 'save_post_type_example_meta_boxes'));
function save_post_type_example_meta_boxes($post_id)
..........
if (...) {
$new_custom_post = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'custom_post_type',
'meta_input' => array(
'meta_1' => 'X',
'parent_post_id' => ???
),
);
wp_insert_post($new_custom_post );
}
update_post_meta($post_id, 'child_post_id', ???);
......
if(isset($_POST['meta_x_post'])) {
$meta_x = sanitize_text_field($_POST['meta_x_post']);
update_post_meta($post_id, 'meta_x', $meta_x);
}
...
}
here it could be $post_id (because it's the ID for Parent post)?
Any suggestions how to get Child post ID?
Thank you!
I'working on plugin for custom post types and it's metadata etc. Everything is fine so far but now I need to extend this plugin - I need to create one extra the same custom post on save/edit post when certain metadata has value X. And I'm stucked to figure out how to get 2 values: 1)Insert "Parent" post ID to "Child" post to specific metadata 2)Insert "Child" post ID to "Parent" post to specific metadata
Here is code:
add_action('save_post', array($this, 'save_post_type_example_meta_boxes'));
function save_post_type_example_meta_boxes($post_id)
..........
if (...) {
$new_custom_post = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'custom_post_type',
'meta_input' => array(
'meta_1' => 'X',
'parent_post_id' => ???
),
);
wp_insert_post($new_custom_post );
}
update_post_meta($post_id, 'child_post_id', ???);
......
if(isset($_POST['meta_x_post'])) {
$meta_x = sanitize_text_field($_POST['meta_x_post']);
update_post_meta($post_id, 'meta_x', $meta_x);
}
...
}
here it could be $post_id (because it's the ID for Parent post)?
Any suggestions how to get Child post ID?
Thank you!
Share Improve this question asked Oct 19, 2018 at 9:18 Mole_LRMole_LR 32 bronze badges 1- When you insert a post, the ID of the new post is returned. – Milo Commented Oct 19, 2018 at 12:43
1 Answer
Reset to default 0Here's an updated version of your code:
add_action('save_post', array($this, 'save_post_type_example_meta_boxes'));
function save_post_type_example_meta_boxes($post_id)
..........
if (...) {
$new_custom_post = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'custom_post_type',
'meta_input' => array(
'meta_1' => 'X',
'parent_post_id' => $post_id,
),
);
// Prevent this action from running twice.
remove_action( 'save_post', [ $this, 'save_post_type_example_meta_boxes' ] );
$child_post_id = wp_insert_post( $new_custom_post );
// Readd the action.
add_action('save_post', array($this, 'save_post_type_example_meta_boxes'));
}
update_post_meta($post_id, 'child_post_id', $child_post_id);
......
if(isset($_POST['meta_x_post'])) {
$meta_x = sanitize_text_field($_POST['meta_x_post']);
update_post_meta($post_id, 'meta_x', $meta_x);
}
...
}
And here's what I changed:
- The incoming post ID to
save_post_type_example_meta_boxesshould be what you want to use as your "parent" post ID. - You should temporarily remove your hook to
save_postso that it doesn't try to run on that action over and over again. - As noted by @Milo,
wp_insert_postreturns a post ID on success, so that would give you the "child" post ID.
I'working on plugin for custom post types and it's metadata etc. Everything is fine so far but now I need to extend this plugin - I need to create one extra the same custom post on save/edit post when certain metadata has value X. And I'm stucked to figure out how to get 2 values: 1)Insert "Parent" post ID to "Child" post to specific metadata 2)Insert "Child" post ID to "Parent" post to specific metadata
Here is code:
add_action('save_post', array($this, 'save_post_type_example_meta_boxes'));
function save_post_type_example_meta_boxes($post_id)
..........
if (...) {
$new_custom_post = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'custom_post_type',
'meta_input' => array(
'meta_1' => 'X',
'parent_post_id' => ???
),
);
wp_insert_post($new_custom_post );
}
update_post_meta($post_id, 'child_post_id', ???);
......
if(isset($_POST['meta_x_post'])) {
$meta_x = sanitize_text_field($_POST['meta_x_post']);
update_post_meta($post_id, 'meta_x', $meta_x);
}
...
}
here it could be $post_id (because it's the ID for Parent post)?
Any suggestions how to get Child post ID?
Thank you!
I'working on plugin for custom post types and it's metadata etc. Everything is fine so far but now I need to extend this plugin - I need to create one extra the same custom post on save/edit post when certain metadata has value X. And I'm stucked to figure out how to get 2 values: 1)Insert "Parent" post ID to "Child" post to specific metadata 2)Insert "Child" post ID to "Parent" post to specific metadata
Here is code:
add_action('save_post', array($this, 'save_post_type_example_meta_boxes'));
function save_post_type_example_meta_boxes($post_id)
..........
if (...) {
$new_custom_post = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'custom_post_type',
'meta_input' => array(
'meta_1' => 'X',
'parent_post_id' => ???
),
);
wp_insert_post($new_custom_post );
}
update_post_meta($post_id, 'child_post_id', ???);
......
if(isset($_POST['meta_x_post'])) {
$meta_x = sanitize_text_field($_POST['meta_x_post']);
update_post_meta($post_id, 'meta_x', $meta_x);
}
...
}
here it could be $post_id (because it's the ID for Parent post)?
Any suggestions how to get Child post ID?
Thank you!
Share Improve this question asked Oct 19, 2018 at 9:18 Mole_LRMole_LR 32 bronze badges 1- When you insert a post, the ID of the new post is returned. – Milo Commented Oct 19, 2018 at 12:43
1 Answer
Reset to default 0Here's an updated version of your code:
add_action('save_post', array($this, 'save_post_type_example_meta_boxes'));
function save_post_type_example_meta_boxes($post_id)
..........
if (...) {
$new_custom_post = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'custom_post_type',
'meta_input' => array(
'meta_1' => 'X',
'parent_post_id' => $post_id,
),
);
// Prevent this action from running twice.
remove_action( 'save_post', [ $this, 'save_post_type_example_meta_boxes' ] );
$child_post_id = wp_insert_post( $new_custom_post );
// Readd the action.
add_action('save_post', array($this, 'save_post_type_example_meta_boxes'));
}
update_post_meta($post_id, 'child_post_id', $child_post_id);
......
if(isset($_POST['meta_x_post'])) {
$meta_x = sanitize_text_field($_POST['meta_x_post']);
update_post_meta($post_id, 'meta_x', $meta_x);
}
...
}
And here's what I changed:
- The incoming post ID to
save_post_type_example_meta_boxesshould be what you want to use as your "parent" post ID. - You should temporarily remove your hook to
save_postso that it doesn't try to run on that action over and over again. - As noted by @Milo,
wp_insert_postreturns a post ID on success, so that would give you the "child" post ID.
本文标签:
版权声明:本文标题:plugins - On save_post need to wp_insert_post and save partent post id to child post and child post id to parent post 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749248610a2339313.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论