Our code base has a ton of logic that executes as the post is inserted/created. However, some of that logic depends on custom post meta. The only way I know of to add post meta to a new post is like so:
$post_id = wp_insert_post($post_obj);
add_post_meta($post_id, 'key', "value");
However, this means that the post meta is not present when hooks on post insertion happen.
Is there any way to set up or include post meta as part of $post_obj
?
I tried making up new properties of the post object with $post_obj->custom_key = 'value'
but it didn't seem to actually end up in the database. The only thing I can think of is to hijack an existing property of the post object that I'm not using, like menu_order
, and store some information there. That's an ugly hack.
Our code base has a ton of logic that executes as the post is inserted/created. However, some of that logic depends on custom post meta. The only way I know of to add post meta to a new post is like so:
$post_id = wp_insert_post($post_obj);
add_post_meta($post_id, 'key', "value");
However, this means that the post meta is not present when hooks on post insertion happen.
Is there any way to set up or include post meta as part of $post_obj
?
I tried making up new properties of the post object with $post_obj->custom_key = 'value'
but it didn't seem to actually end up in the database. The only thing I can think of is to hijack an existing property of the post object that I'm not using, like menu_order
, and store some information there. That's an ugly hack.
2 Answers
Reset to default 2You can hook your function to the wp_insert_post
action hook:
add_action( 'wp_insert_post', 'wpse128767_add_meta' );
function wpse128767_add_meta( $post_id ) {
add_post_meta( $post_id, 'key', 'value' );
}
To make sure your metadata has been added before any other insert hooks run, give it a higher priority:
add_action( 'wp_insert_post', 'wpse128767_add_meta', 1 );
By default add_action()
uses a priority of 10
; the lower the number, the earlier it runs.
Here is a solution.
//Your args to Gather post data.
$args = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
);
//Retrieve Id back after new post is made.
$post_id = wp_insert_post( $args, true );
//Check for errors, before updating meta fields
if (is_wp_error($post_id)):
$errors = $post_id->get_error_messages();
foreach ($errors as $error):
//log errors
echo $error;
endforeach;
else:
//If updating WP meta
update_post_meta( $post_id, meta_key1, 'add this');
update_post_meta( $post_id, meta_key2, 'add something');
//if updating ACF meta
//update_field( 'meta_key1', 'add this', $post_id );
//update_field( 'meta_key2', 'add something', $post_id );
endif;