最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Notice: map_meta_cap was called incorrectly

programmeradmin2浏览0评论

I created an external script to import json data into a custom post type that uses wp-load.php. Everything works fine but after every update I get the message

Notice: map_meta_cap was called incorrectly. The post type EXAMPLE is not registered, so it may not be reliable to check the capability "edit_post" against a post of that type.

Im calling the script inside a child theme and post_type_exists returns false but works fine...

Ive seen a similar notice in a core track thread but it talks about comments being the issue and my post type doesnt have any comment functionality.

function child_post_types() {

$labels = array(
    'name'                => _x( 'Courses', 'Post Type General Name', 'test' ),
    'singular_name'       => _x( 'Course', 'Post Type Singular Name', 'test' ),
    'menu_name'           => __( 'Courses', 'test' ),
    'parent_item_colon'   => __( 'Parent Course:', 'test' ),
    'all_items'           => __( 'All Courses', 'test' ),
    'view_item'           => __( 'View Course', 'test' ),
    'add_new_item'        => __( 'Add New Course', 'test' ),
    'add_new'             => __( 'Add New', 'test' ),
    'edit_item'           => __( 'Edit Course', 'test' ),
    'update_item'         => __( 'Update Course', 'test' ),
    'search_items'        => __( 'Search Courses', 'test' ),
    'not_found'           => __( 'Not found', 'test' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'test' ),
);
$args = array(
    'label'               => __( 'Course', 'test' ),
    'description'         => __( 'Courses', 'test' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'thumbnail', 'editor', 'revisions', 'author' ), //editor, thumbnail, title, author, excerpt, trackpacks, custom-fields, comments, revisions, page-attributes, post-formats 
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'rewrite'             =>array('slug'=>'courses'),
    'capability_type'     => 'page',
);
register_post_type( 'course', $args );

}    
add_action( 'init', 'child_post_types', 0 );

And the script is sitting in my child theme just getting a local json file and looping through courses with wp_update_post()

require('../../../wp-load.php');

$data = json_decode( file_get_contents( 'wp-content/themes/_theme/courses.json' ), true );

foreach($data as $key => $c){

  $course = wp_update_post( array(
                'ID'    => $courseID, 
                'post_title' => $c['CourseTitle'],
                'post_content' => $c['Description'],
            ), true );
}

Any ideas?

I created an external script to import json data into a custom post type that uses wp-load.php. Everything works fine but after every update I get the message

Notice: map_meta_cap was called incorrectly. The post type EXAMPLE is not registered, so it may not be reliable to check the capability "edit_post" against a post of that type.

Im calling the script inside a child theme and post_type_exists returns false but works fine...

Ive seen a similar notice in a core track thread but it talks about comments being the issue and my post type doesnt have any comment functionality.

function child_post_types() {

$labels = array(
    'name'                => _x( 'Courses', 'Post Type General Name', 'test' ),
    'singular_name'       => _x( 'Course', 'Post Type Singular Name', 'test' ),
    'menu_name'           => __( 'Courses', 'test' ),
    'parent_item_colon'   => __( 'Parent Course:', 'test' ),
    'all_items'           => __( 'All Courses', 'test' ),
    'view_item'           => __( 'View Course', 'test' ),
    'add_new_item'        => __( 'Add New Course', 'test' ),
    'add_new'             => __( 'Add New', 'test' ),
    'edit_item'           => __( 'Edit Course', 'test' ),
    'update_item'         => __( 'Update Course', 'test' ),
    'search_items'        => __( 'Search Courses', 'test' ),
    'not_found'           => __( 'Not found', 'test' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'test' ),
);
$args = array(
    'label'               => __( 'Course', 'test' ),
    'description'         => __( 'Courses', 'test' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'thumbnail', 'editor', 'revisions', 'author' ), //editor, thumbnail, title, author, excerpt, trackpacks, custom-fields, comments, revisions, page-attributes, post-formats 
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'rewrite'             =>array('slug'=>'courses'),
    'capability_type'     => 'page',
);
register_post_type( 'course', $args );

}    
add_action( 'init', 'child_post_types', 0 );

And the script is sitting in my child theme just getting a local json file and looping through courses with wp_update_post()

require('../../../wp-load.php');

$data = json_decode( file_get_contents( 'wp-content/themes/_theme/courses.json' ), true );

foreach($data as $key => $c){

  $course = wp_update_post( array(
                'ID'    => $courseID, 
                'post_title' => $c['CourseTitle'],
                'post_content' => $c['Description'],
            ), true );
}

Any ideas?

Share Improve this question edited Aug 5, 2016 at 18:58 chris asked Aug 5, 2016 at 15:32 chrischris 1711 gold badge1 silver badge7 bronze badges 12
  • it says The post type EXAMPLE is not registered. where come this CPT from ? – mmm Commented Aug 5, 2016 at 15:35
  • @mmm its just an example the actual post type is called course and its defined in my child theme loaded into functions.php – chris Commented Aug 5, 2016 at 16:07
  • Without seeing some real code it's a bit hard to suggest anything – Andy Macaulay-Brook Commented Aug 5, 2016 at 17:05
  • @AndyMacaulay-Brook Seems like such basic code I thought maybe this is a new bug or something... I updated the question – chris Commented Aug 5, 2016 at 18:49
  • you dont' have used "map_meta_cap" in the CPT declaration, set it to TRUE : codex.wordpress.org/Function_Reference/… – mmm Commented Aug 5, 2016 at 19:04
 |  Show 7 more comments

4 Answers 4

Reset to default 1

Today i got exactly the same behaviour when adding a custom post type. I also noticed some admin post search tools stopped working. My debug_log showed exactly the same notice.

The problem turned out to be some text before the <?php opening tag (top file) were I register the CPT.

Example

some text here<?php
/**
 * @class My_Cpt_Class
 */
.......

The CPT does get registered correctly. I can add posts etc. But a few default post actions (from other plugins aswell) stop working for ALL post types. I have no idea why this happens with this error.

You get this error if a post is encountered with a post type that you haven't registered on the init event.

I always call register_post_type() on init, as instructed in the docs. However, in my dev build process I use wp-cli to deactivate my plugin, and re-install the new iteration of the plugin. In the brief moment when the plugin is deactivated, this message appears because WP runs without the post type being registered.

In short, WordPress shows this error whenever it sees a post with a post type that's not properly registered.

There are many reasons for this kind of error.

One of the reasons that I faced was when I had data saved in comments tables but those plugins that created those data wasn't activated so the debugging tool was showing this kind of error after activating the plugin the error was gone.

By looking into your case I think it's the priority issue. Instead of using priority 0, try the default priority 10

add_action( 'init', 'child_post_types', 10 );

That might solve things.

This answer might be old on this post but I hope it can help others:

I had this trouble and I solved it using this.

For example: I used add_action('init', 'register_posttype'); and that gives me the error map_meta_cap incorrectly called. so I changed it to admin_init instead of init.

发布评论

评论列表(0)

  1. 暂无评论