• Scott DeLuzio

    (@scottdeluzio)


    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?

Viewing 1 replies (of 1 total)
  • Thread Starter Scott DeLuzio

    (@scottdeluzio)

    I don’t know if this is the best solution or not, but I have the post meta updated by putting update_post_meta() before the variables with get_post_meta are defined like this.

    update_post_meta($ID,'task-assignment',$_POST['task-assignment']);
    update_post_meta($ID,'task-due-date',$_POST['task-due-date']);

    If I’m looking at it correctly, this makes the post meta get updated with the last inputs before clicking Publish or Update. Then when the variables are defined with get_post_meta, they are using the most recent values for the post meta.

    If anyone sees anything wrong with this, or knows of a better way please let me know.

Viewing 1 replies (of 1 total)
  • The topic ‘get_post_meta in {status}_{post_type} Hook’ is closed to new replies.