Skip to main content
use `objects`
Source Link
chrisguitarguy
  • 21.5k
  • 5
  • 62
  • 99

@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'objects' 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$type->rewrite->slug ) )
        {
            // you'll probably want to do something else.
            echo $typeobj$type->rewrite->slug;
        }
    }
}

@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;
        }
    }
}

@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 = get_post_types( [], 'objects' );
    foreach ( $types as $type ) {
        if ( isset( $type->rewrite->slug ) ) {
            // you'll probably want to do something else.
            echo $type->rewrite->slug;
        }
    }
}
Source Link
chrisguitarguy
  • 21.5k
  • 5
  • 62
  • 99

@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;
        }
    }
}