Edit:
get_post_type_labels was never intended to be a public function. It is only used internally when registering a post type. See these trac tickets:
- get_taxonomy_labels() and _get_custom_object_labels() fail if $object->taxonomy is not array
- Fatal error: Cannot use object of type stdClass as array in /…/wp-includes/post.php on line 1202
Like you mentioned in your question you can get the labels directly from the globals:
function c3m_get_labels() {
global $wp_post_types;
$post_type_name = get_current_screen ()->post_type;
$labels = &$wp_post_types[$post_type_name]->labels;
return $labels;
}
Running the function:
$labels = c3m_get_labels();
var_dump($labels);
Returns:
object(stdClass)[232]
public 'name' => string 'Posts' (length=5)
public 'singular_name' => string 'Post' (length=4)
public 'add_new' => string 'Add New' (length=7)
public 'add_new_item' => string 'Add New Post' (length=12)
public 'edit_item' => string 'Edit Post' (length=9)
public 'new_item' => string 'New Post' (length=8)
public 'view_item' => string 'View Post' (length=9)
public 'search_items' => string 'Search Posts' (length=12)
public 'not_found' => string 'No posts found.' (length=15)
public 'not_found_in_trash' => string 'No posts found in Trash.' (length=24)
public 'parent_item_colon' => null
public 'all_items' => string 'All Posts' (length=9)
public 'menu_name' => string 'Posts' (length=5)
public 'name_admin_bar' => string 'Post'
Alternate use without needing global $wp_post_types:
function c3m_get_labels() {
$post_type_name = get_current_screen ()->post_type;
$post_type_obj = get_post_type_object ( $post_type_name );
return $post_type_obj;
}
Running the function:
$labels = c3m_get_labels();
$labels = $labels->labels;
var_dump($labels);
After doing several tests I have concluded that it is impossible to use get_post_type_labels passing the $post_type_object as specified in the codex. So it seems the only use for get_post_type_labels
is internally in core when a post type is registered.