Support » Fixing WordPress » Meta box stopped working after upgrading to wordpress 5

  • Resolved jogesh_pi

    (@jogesh_pi)


    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.

    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
    }

    Custom Post Type

    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();
    }
  • The topic ‘Meta box stopped working after upgrading to wordpress 5’ is closed to new replies.