get post type plural

Currently I’m trying to get the post type labels plural. In detail:

$GLOBALS['wp_post_types'][ get_current_screen()->post_type ]->labels->name

This is how I’m trying to retrieve it – in the admin UI – using the public wp API functions:

$post_type_name = get_current_screen()->post_type;
$post_type_obj = get_post_type_object( $post_type_name );
get_post_type_labels( $post_type_obj );

The result is the following Error:

Fatal error: Cannot use object of type stdClass as array in D:\development\xampp\htdocs\wp_inst\wp-includes\post.php on line 1209

The problem is that the post type objects labels is also an object, while get_post_type_labels() calls _get_custom_object_labels() internally and seems to expect an array.

Fun are the last two lines of the _get_custom_object_labels() function:

$labels = array_merge( $defaults, $object->labels );
return (object)$labels;

So, am I using the wrong functions? Is the core function wrong? Is this core function only meant to be used during custom post type registration?


Note: Currently on WP version 3.3.2.

3 s
3

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.

Leave a Comment