0

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();
}
  • And... 1. How is your CPT registered? 2. Where do you add this metabox? – Krzysiek Dróżdż Feb 18 at 6:29
  • @KrzysiekDróżdż, I have updated the code. – jogesh_pi Feb 18 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ż Feb 18 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 Feb 18 at 6:44
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.
  • I did not recognize this. Then surely post_type is a reserved variable on the upgraded version. Thank you so much for your help. – jogesh_pi Feb 18 at 7:30
  • 1
    It always was, I guess. You had luck, if it was working... – Krzysiek Dróżdż Feb 18 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.