get_post_meta in {status}_{post_type} Hook
-
I’m trying to access some post meta information using a {status}_{post_type} Hook.
The (simplified) function I have looks like this:function update_cpt_info( $ID, $post ){ $author = $post->post_author; /* Post author ID. */ $author_name = get_the_author_meta( 'display_name', $author ); $title = $post->post_title; $user = get_post_meta( $ID, 'task-assignment', true ); $username = get_user_by('login',$user); $assigned = $username->display_name; $duedate = date('MM dd, yy',htmlspecialchars(get_post_meta( $ID, 'task-due-date', true ) ) ); // do stuff with these results... } add_action( 'publish_taskcpt', 'update_cpt_info', 10, 2 );
When I publish a new taskcpt post, I get a notice on the
$assigned
line saying “Notice: Trying to get property of non-object”, and on the$duedate
line saying “Warning: date() expects parameter 2 to be long, string”. The post$author
and$title
do display correctly. It seems like just the post meta in both of these cases just doesn’t exist when the function is looking for it.When I update an existing taskcpt, the function works with no errors or warnings, but the output has the previous post meta information.
I’m guessing that the {status}_{post_type} hook fires too soon so the post meta hasn’t had a chance to be populated in the database on a new post, and it hasn’t had a chance to get updated on an existing post.
If this is the case, is there another way to access the post meta or somehow delay the execution of this function?
- The topic ‘get_post_meta in {status}_{post_type} Hook’ is closed to new replies.