0

Why if I register a custom post type by add_action( 'init', 'custom_post_type' ) hook it doesn't show in returned array by get_post_types function? Example:

function test(){
    $args = array(
            'public'          => true,
            'publicly_queryable' => true,
            'show_ui'         => true,
            'query_var'       => true,
            'exclude_from_search' => true,
            'hierarchical'    => false,
            'has_archive'     => false,

        ); 

        register_post_type( 'test', $args );
}
add_action( 'init', 'test' );
print_r(get_post_types()); 
Array
(
    [post] => post
    [page] => page
    [attachment] => attachment
    [revision] => revision
    [nav_menu_item] => nav_menu_item
    //no test post type in array
)

but if I just run function test() the post type test appears in get_post_type array.

0

Because you calling get_post_types() in functions.php that is excuted before 'init' action. Look this picture http://www.rarst.net/images/wordpress_core_load.png

if you add an echo inside function test

function test(){
    $args = array(
        'public'          => true,
        'publicly_queryable' => true,
        'show_ui'         => true,
        'query_var'       => true,
        'exclude_from_search' => true,
        'hierarchical'    => false,
        'has_archive'     => false,

    ); 

    register_post_type( 'test', $args );

    echo 'HELLO!!';

}
add_action( 'init', 'test' );

print_r(get_post_types()); 

You'll see before the array of custom fields and then 'Hello!" string!

  • So what you can recommend me to fix this problem? How is the best way to register custom post type? – Michał Kalkowski Mar 27 '14 at 12:48
  • @user853543: I suppose you didn't see my answer. I said exactly the same a few minutes before you. – cybmeta Mar 27 '14 at 13:00
  • I saw it after that I published my comment! I'm sorry! – kream Mar 27 '14 at 14:50
  • @MichałKalkowski you can try to change the hook add_action( 'init', 'test' ); to add_action( 'setup_theme', 'test' ); – kream Mar 27 '14 at 20:12
  • @kream no this doesn't work the custom post type isn't registerd. – Michał Kalkowski Mar 28 '14 at 11:19
1

In your code, you are executing get_post_types() before the post type is registered. Try, for example, this:

function test(){
    $args = array(
        'public'          => true,
        'publicly_queryable' => true,
        'show_ui'         => true,
        'query_var'       => true,
        'exclude_from_search' => true,
        'hierarchical'    => false,
        'has_archive'     => false,

    ); 

    register_post_type( 'test', $args );

    print_r(get_post_types());

}
add_action( 'init', 'test' );

Similar results you will get if you call get_post_types() in your template or any other situation that come after the Wordpress init event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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