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?
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.
That fixed it. Thank you!