$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>metabox - Custom post type suddenly stoped working after WordPress 5 update|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

metabox - Custom post type suddenly stoped working after WordPress 5 update

matteradmin9PV0评论

I build a simple plugin that has a CPT and 1 metabox section that collect extra information from admin. It was working fine in the old version but when i just updated it to WordPress 5 it stopped working.

I tried to figure it out and when I comment the Meta Box input fields then my CPT works fine. Otherwise, I got the following error.

A post type mismatch has been detected.

I event don't find any help over the web if i missed something.

These are my metabox codes:

// add_action('add_meta_boxes', array($this, 'answer_meta_action'));
add_action('admin_menu', array($this, 'answer_meta_action'));

public function answer_meta_action() {
    add_meta_box( 
        'ttl-to-edit', 
        __('Edit In Response To', 'ttl-p'), 
        array($this, 'response_to_edit'), 
        'ttl', // Custom Post Type 
        'normal', 
        'high'
    );
}

public function response_to_edit()
{
    $post_types = get_post_types();
    ?>
    <style>.clearfix {overflow: auto;}.clearfix::after {content: "";clear: both;display: table;}</style>
    <span class="tsqa_response_to_action-error" style="color:#ff0000;"></span>
    <div id="tsqa_response_to_action">
        <div style="margin: 10px 0 10px 0;" class="clearfix">
            <div class="box-section" style="width:250px;float:left;">
                <strong><label>Post Type:</label></strong> <br>
                <select name="post_type" style="width: 200px;margin-right:20px;">
                    <option value="">- Select -</option>
                    <?php foreach( $post_types as $post_type ): ?>
                        <option value="<?php echo $post_type ?>"><?php echo $post_type ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
    </div>
    <?php
}

CPT Register:

add_action('init', array($this, 'init'));
public function init() {


    $labels = array(
        'name' => _x( 'Question', 'Post Type General Name', '' ),
        ....
    );



    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Questions', '' ),
        'public'             => false,
        'publicly_queryable' => false,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'menu_position'      => 5,
        'menu_icon'          => 'dashicons-format-status',
        'query_var'          => true,
        'rewrite'            => array('slug'=>'question'),
        'capability_type'    => 'post',
        'capabilities'       => array(
            'create_posts'       => 'do_not_allow', 
        ), 
        'map_meta_cap'       => true,
        'has_archive'        => 'questions',
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor' ), 
    );


    register_post_type( 'ttl', $args );
    flush_rewrite_rules();
}

I build a simple plugin that has a CPT and 1 metabox section that collect extra information from admin. It was working fine in the old version but when i just updated it to WordPress 5 it stopped working.

I tried to figure it out and when I comment the Meta Box input fields then my CPT works fine. Otherwise, I got the following error.

A post type mismatch has been detected.

I event don't find any help over the web if i missed something.

These are my metabox codes:

// add_action('add_meta_boxes', array($this, 'answer_meta_action'));
add_action('admin_menu', array($this, 'answer_meta_action'));

public function answer_meta_action() {
    add_meta_box( 
        'ttl-to-edit', 
        __('Edit In Response To', 'ttl-p'), 
        array($this, 'response_to_edit'), 
        'ttl', // Custom Post Type 
        'normal', 
        'high'
    );
}

public function response_to_edit()
{
    $post_types = get_post_types();
    ?>
    <style>.clearfix {overflow: auto;}.clearfix::after {content: "";clear: both;display: table;}</style>
    <span class="tsqa_response_to_action-error" style="color:#ff0000;"></span>
    <div id="tsqa_response_to_action">
        <div style="margin: 10px 0 10px 0;" class="clearfix">
            <div class="box-section" style="width:250px;float:left;">
                <strong><label>Post Type:</label></strong> <br>
                <select name="post_type" style="width: 200px;margin-right:20px;">
                    <option value="">- Select -</option>
                    <?php foreach( $post_types as $post_type ): ?>
                        <option value="<?php echo $post_type ?>"><?php echo $post_type ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
    </div>
    <?php
}

CPT Register:

add_action('init', array($this, 'init'));
public function init() {


    $labels = array(
        'name' => _x( 'Question', 'Post Type General Name', '' ),
        ....
    );



    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Questions', '' ),
        'public'             => false,
        'publicly_queryable' => false,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'menu_position'      => 5,
        'menu_icon'          => 'dashicons-format-status',
        'query_var'          => true,
        'rewrite'            => array('slug'=>'question'),
        'capability_type'    => 'post',
        'capabilities'       => array(
            'create_posts'       => 'do_not_allow', 
        ), 
        'map_meta_cap'       => true,
        'has_archive'        => 'questions',
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor' ), 
    );


    register_post_type( 'ttl', $args );
    flush_rewrite_rules();
}
Share Improve this question edited Feb 18, 2019 at 6:33 jogesh_pi asked Feb 18, 2019 at 6:22 jogesh_pijogesh_pi 2674 silver badges16 bronze badges 4
  • And... 1. How is your CPT registered? 2. Where do you add this metabox? – Krzysiek Dróżdż Commented Feb 18, 2019 at 6:29
  • @KrzysiekDróżdż, I have updated the code. – jogesh_pi Commented Feb 18, 2019 at 6:36
  • OK, so first of all... You should NEVER put flush_rewrite_rules in init hook - it will cause major performance issues. Let me take a look at the rest of your code... – Krzysiek Dróżdż Commented Feb 18, 2019 at 6:41
  • @KrzysiekDróżdż Thank you, Will keep in mind for the future. I tried to debug the codes but didn't get any error log. Just only get the white screen with A post type mismatch has been detected. – jogesh_pi Commented Feb 18, 2019 at 6:44
Add a comment  | 

1 Answer 1

Reset to default 1

OK, I had to debug it and... Everything is just fine with your code. Almost everything.

When I click the Publish button, I get that error:

A post type mismatch has been detected

And if you'll take a look at your form, you'll see why...

In your form you have this field:

<select name="post_type" style="width: 200px;margin-right:20px;">

post_type name is already used for different purposes. So your field overwrites it and is causing the error.

There are some names that you should be avoided, when you create your own meta boxes. And the best idea is to prepend your fields with some prefix to make sure, you're not causing any conflicts.

So if you add ttl-to-edit meta box, I'd call all the fields like ttl-to-edit-post_type or something like this. Sometimes it's convenient to put them in an array like ttl_to_edit[post_type].

PS. You should NEVER put flush_rewrite_rules in init hook - it will cause major performance issues.

  • Flushing the rewrite rules is an expensive operation, there are tutorials and examples that suggest executing it on the 'init' hook. This is bad practice. It should be executed either on the 'shutdown' hook, or on plugin/theme (de)activation.
  • Flush rules once (it's better to store the state in option, instead of activation or deactivation, because on MultiSite they are useless), or when you know that the rewrite rules need to be changed. Don't do it on any hook that will triggered on a routine basis.
Post a comment

comment list (0)

  1. No comments so far