Skip Navigation

[Resolved] get_post_type_object function returns null in init action

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use the WP function get_post_type_object in my init hook, but it returns empty or null for my custom post types.

Solution: Try priority 15, when most of Types functionality should be available.

function myplugin_register_template() {
    $post_type_object = get_post_type_object( 'book' );
}
add_action( 'init', 'myplugin_register_template', 15 );
This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by marketingS 4 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1392481

I am trying to: Create a block template for a custom post type that I created with Toolset Types. I am using the get_post_type_object function the init action for a custom post type.

I expected to see: The template of blocks for my custom post type

Instead, I got: An error saying the object was null.

I'm assuming this action is being called before Toolset's register_post_type action. How can this be resolved?

#1392765

Hi, what priority level are you using in the init hook? Try 15, at least:

function myplugin_register_template() {
    $post_type_object = get_post_type_object( 'book' );
    error_log(print_r($post_type_object, true));
}
add_action( 'init', 'myplugin_register_template', 15 );

Results in my local test:

[26-Nov-2019 22:20:56 UTC] WP_Post_Type Object
(
    [name] => book
    [label] => Books
    [labels] => stdClass Object
        (
            [name] => Books
            [singular_name] => Book
            [add_new] => Add New
            [add_new_item] => Add New Book
            [edit_item] => Edit Book
            [new_item] => New Book
            [view_item] => View Book
            [view_items] => View Posts
            [search_items] => Search Books
            [not_found] => No Books found
            [not_found_in_trash] => No Books found in Trash
            [parent_item_colon] => Parent text
            [all_items] => All items
            [archives] => All items
            [attributes] => Post Attributes
            [insert_into_item] => Insert into post
            [uploaded_to_this_item] => Uploaded to this post
            [featured_image] => Featured Image
            [set_featured_image] => Set featured image
            [remove_featured_image] => Remove featured image
            [use_featured_image] => Use as featured image
            [filter_items_list] => Filter posts list
            [items_list_navigation] => Posts list navigation
            [items_list] => Posts list
            [item_published] => Post published.
            [item_published_privately] => Post published privately.
            [item_reverted_to_draft] => Post reverted to draft.
            [item_scheduled] => Post scheduled.
            [item_updated] => Post updated.
            [menu_name] => Books
            [enter_title_here] => Enter title here
            [name_admin_bar] => Book
        )

    [description] =>
    [public] => 1
    [hierarchical] =>
    [exclude_from_search] =>
    [publicly_queryable] => 1
    [show_ui] => 1
    [show_in_menu] => 1
    [show_in_nav_menus] => 1
    [show_in_admin_bar] => 1
    [menu_position] =>
    [menu_icon] => dashicons-admin-post
    [capability_type] => post
    [map_meta_cap] => 1
    [register_meta_box_cb] =>
    [taxonomies] => Array
        (
        )

    [has_archive] => 1
    [query_var] => book
    [can_export] => 1
    [delete_with_user] =>
    [_builtin] =>
    [_edit_link] => post.php?post=%d
    [cap] => stdClass Object
        (
            [edit_post] => edit_post
            [read_post] => read_post
            [delete_post] => delete_post
            [edit_posts] => edit_posts
            [edit_others_posts] => edit_others_posts
            [publish_posts] => publish_posts
            [read_private_posts] => read_private_posts
            [read] => read
            [delete_posts] => delete_posts
            [delete_private_posts] => delete_private_posts
            [delete_published_posts] => delete_published_posts
            [delete_others_posts] => delete_others_posts
            [edit_private_posts] => edit_private_posts
            [edit_published_posts] => edit_published_posts
            [create_posts] => edit_posts
        )

    [rewrite] => Array
        (
            [enabled] => 1
            [with_front] => 1
            [feeds] => 1
            [pages] => 1
            [slug] => book
            [ep_mask] => 1
        )

    [show_in_rest] => 1
    [rest_base] => book
    [rest_controller_class] =>
    [rest_controller] =>
    [_toolset_edit_last] => 1567011503
    [_wpcf_author_id] => 1
    [wpcf-post-type] => book
    [icon] => admin-post
    [slug] => book
    [editor] => per_post
    [custom-field-group] => Array
        (
            [74] => 1
            [27] => 1
        )

    [has_archive_slug] =>
    [show_in_menu_page] =>
    [query_var_enabled] => 1
    [permalink_epmask] => 1
    [show_in_rest_control] => 1
    [show_relationship] => Array
        (
            [book-chapter] => on
            [speaker-book] => on
        )

    [is_repeating_field_group] =>
    [is_intermediary_post_type] =>
    [__accessIsCapValid] => 1
    [__accessIsNameValid] => 1
    [__accessNewCaps] => stdClass Object
        (
            [edit_post] => edit_book
            [read_post] => read_book
            [delete_post] => delete_book
            [edit_posts] => edit_books
            [edit_others_posts] => edit_others_books
            [publish_posts] => publish_books
            [read_private_posts] => read_private_books
            [read] => read
            [delete_posts] => delete_books
            [delete_private_posts] => delete_private_books
            [delete_published_posts] => delete_published_books
            [delete_others_posts] => delete_others_books
            [edit_private_posts] => edit_private_books
            [edit_published_posts] => edit_published_books
            [create_posts] => edit_books
        )

    [_wpcf_access_inherits_post_cap] => 1
)

I can't speak to the effectiveness of modifying a Block Template for a CPT though, I'm not very familiar with that. I'm not sure what is locked and what is open to extension. Let me know what you find out, and I can ask for more information from my developers if necessary.

#1392977

That fixed it. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.