Notice: map_meta_cap was called incorrectly

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?

Solutions

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.

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.

Similar questions

string(2) "id" Notice: id was called incorrectly. Product properties should not be accessed directly. Backtrace:
string(2) "id" Notice: id was called incorrectly. Product properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/plugins/woocommerce/templates/single-product.php'), wc_get_template_part, load_template, require('/plugins/woocommerce/templates/content-...
Getting notice is wordpress "WP_Scripts::localize was called incorrectly"
Complete message: Notice: WP_Scripts::localize was called incorrectly. The $l10n parameter must be an array. To pass arbitrary data to scripts, use the wp_add_inline_script() function instead. Please see Debugging in WordPress for more information. (This message was added in version 5.7.0.) in /home3/dduconne/public_html/wp-includes/functions.php o...
Notice: register_rest_route was called incorrectly. The REST API route definition for yoast/v1/get_head is missing the required permission_callback a
The site I'm working on is showing this issue on the posts/pages on the dashboard. "Notice: register_rest_route was called incorrectly. The REST API route definition for yoast/v1/get_head is missing the required permission_callback argument. For REST API routes that are intended to be public, use __return_true as the permission callback. "...
What Is The Use Of map_meta_cap Filter?
What is the use of map_meta_cap filter? This filter is not documented anywhere. I have an unclear idea of what it could be: Used to map the permissions of the user to operations on posts. What exactly is it meant for? If possible please give some sample code example of its right usage.
Custom Post Type's Capabilities map_meta_cap issues
I've created a CPT, added capabilities to it and them to the Subscriber role successfully. However the mapping is clearly not working because I get errors on publish and a Subscriber cannot edit their own posts (which the function below allows if they are the author). Why won't my map_meta_cap function work? I've spent all weekend on this so I have...
map_meta_cap woes
I have created a custom post type and want it to behave like a page. Certain roles need to have access to it and not to posts or pages. So I created my custom post type with a capability-type and capabilities like so: And am assigning them to various roles like so: Upon further research I've found I needed to include a map_meta_cap()function which ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .