I'd like to get a list (array) of all the post types I registered.

Precisely I would like to retrieve their slugs.

Could someone help me? thanks!

link|improve this question

71% accept rate
feedback

2 Answers

up vote 1 down vote accepted

@EAMann's answer is correct, but there's already a build in WordPress function for fetching all registered post types: get_post_types

<?php
// hook into init late, so everything is registered
// you can also use get_post_types where ever.  Any time after init is usually fine.
add_action( 'init', 'wpse34410_init', 0, 99 );
function wpse34410_init() 
{
    // types will be a list of the post type names
    $types = get_post_types();

    // get the registered data about each post type with get_post_type_object
    foreach( $types as $type )
    {
        $typeobj = get_post_type_object( $type );

        // need the actual slug?  this will do it...
        if( isset( $typeobj->rewrite->slug ) )
        {
            // you'll probably want to do something else.
            echo $typeobj->rewrite->slug;
        }
    }
}
link|improve this answer
thank you both guys – Fulvio Nov 22 '11 at 0:43
2  
TIP: You don't need to call get_post_type_object if you set the get_post_types call to return objects to you, eg. $types = get_post_types( '', 'objects' ); – t31os Nov 23 '11 at 9:53
Good call, thanks! – Christopher Davis Nov 23 '11 at 14:41
feedback

When you call register_post_type(), it adds your new post type to a global variable called $wp_post_types. So you can get a list of all of your post types from that:

function get_registered_post_types() {
    global $wp_post_types;

    return array_keys( $wp_post_types );
}

The $wp_post_types variable is an array that contains your CPT definitions, with each set of CPT arguments (labels, capabilities, etc) mapped to the slug of the CPT. Calling array_keys() will give you an array of the slugs of your CPTs.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.