How to get author ID on post.php? $authordata returns NULL (Wordpress)

408 Views Asked by At

I tried the code below on functions.php


function some_func($post_ID) {
    global $authordata;
    if( !is_null($authordata) ) {
        wp_set_object_terms($post_ID,2,'item_cat'); // I want to get this result.
    } else {    
        wp_set_object_terms($post_ID,7,'item_cat'); // $authordata seems to return NULL, and I get this result.
    }
}
add_action('publish_my_post_type','some_func');

Eventually, I want to get a author ID(NOT current user ID for some reason) of editing page.

1

There are 1 best solutions below

1
On

I think you are inside a template like single.php The $authordate is accessible inside the loop, so you may have forgotten to use

if (have_posts()):
    while(have_posts()):
        the_post();
    endwhile;
endif;