marcelo2605
marcelo2605

Reputation: 2794

WordPress: get_post_type_object() return NULL for CPT

The function get_post_type_object() is returning NULL for my CPTs. For default post types, the informations is returned.

Here is one of my CPTs configuration:

function cptui_register_my_cpts_discurso() {

    /**
     * Post Type: Discursos.
     */

    $labels = array(
        "name" => __( "Discursos", "foo" ),
        "singular_name" => __( "Discurso", "foo" ),
    );

    $args = array(
        "label" => __( "Discursos", "foo" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "has_archive" => true,
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "discurso", "with_front" => true ),
        "query_var" => true,
        "menu_icon" => "dashicons-format-chat",
        "supports" => array( "title", "editor", "revisions", "author" ),
    );

    register_post_type( "discurso", $args );
}

add_action( 'init', 'cptui_register_my_cpts_discurso' );

Getting post type object

$obj = get_post_type_object( 'discurso' );

Upvotes: 1

Views: 2960

Answers (1)

Dipak Kumar Pusti
Dipak Kumar Pusti

Reputation: 1723

Try to use it inside an action, it may be init or wp as follows,

function cptui_register_my_cpts_discurso(){
    ....
    ....
    register_post_type( "discurso", $args );

    // Get your object
    $obj = get_post_type_object( 'discurso' );
}
add_action( 'init', 'cptui_register_my_cpts_discurso' );

Or try wp action

function just_another_function(){

    // Get your object
    $obj = get_post_type_object( 'discurso' );
}
add_action( 'wp', 'just_another_function' );

Hope this once helps.

Upvotes: 2

Related Questions