Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers 2

up vote 2 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;
        }
    }
}
share|improve this answer
    
thank you both guys –  nekojira 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! –  chrisguitarguy Nov 23 '11 at 14:41
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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