Cmb2: Not Working

Created on 3 Oct 2016  路  7Comments  路  Source: CMB2/CMB2

Expected Behavior:

The meta box should be added?

Actual Behavior:

I thought I was adding the code so it would show up in my posts but it's not. Does it matter my posts are custom posts? And if so, what code do I need to add so it will show up in custom type posts? I saw the show specific type posts but I still don't get how to work that code so a very specific example according to a post type would be helpful. Right now my metabox doesn't show up at all, whether it's a normal post or custom post type.

Steps to reproduce (I have confirmed I can reproduce this issue on the trunk branch):





    1. CMB2 Field Registration Code:


add_action ( 'cmb2_init', 'cmb2_add_metabox' );
function cmb2_add_metabox() {
    $prefix = 'cmb2';
    $cmb = new_cmb2_box ( array (
        'id'           => $prefix . 'title2',
        'title'        => __('title2', 'cmb2'),
        'context'      => 'normal',
        'priority'     => 'default', )
      );
    $cmb->add_field ( array (
        'name' => __('title2', 'cmb2'),
        'id' => $prefix . 'title2',
        'type' => 'text',
        'default' => 'title', 
     )
);
 }

Most helpful comment

Took me a few minutes to spot this, but it's plural object_types, not singular object_type

function cmb2_hide_if_no_cats( $field ) {
    if ( ! has_tag( 'cats', $field->object_id ) ) {
        return false;
    }

    return true;
}

add_action( 'cmb2_init', 'cmb2_add_metabox' );
function cmb2_add_metabox() {
    $prefix = 'cmb2';
    $cmb    = new_cmb2_box( array(
        'id'       => $prefix . 'title2',
        'title'    => __( 'title2', 'cmb2' ),
        'context'  => 'normal',
        'priority' => 'default',
        'object_types' => array( 'movie' )
    ) );
    $cmb->add_field( array(
        'name'    => __( 'title2', 'cmb2' ),
        'id'      => $prefix . 'title2',
        'type'    => 'text',
        'default' => 'title',
    ) );
}

screen shot 2016-10-03 at 11 37 51 am

Obviously change the "movie" post type I used for my dev site to "review".

Your paste above has an extra }, just so you know. Copy/paste mine here and you should be covered on that topic.

All 7 comments

I'd give https://github.com/WebDevStudios/CMB2/blob/trunk/example-functions.php#L109 a lookover and compare what you have with what the example metaboxes have.

Biggest thing I can see right off the bat is that you don't provide any specific object type, and I don't believe there's a default value for that one.

For object_type, I add in; 'object_types' => array( 'page', ), // Post type

Could I change 'page' to a custom post_type? like 'review' for example and if so, does it need to be the custom post type name or slug (I've tried both, and so far, neither has worked thus far yet).

Also, when I try this code, I am always getting a unexpected => error, which I've googled and they say a) it's because of an array returning empty or b) because I have forgotten a ) ) ; etc but I haven't that I can for b so it must be a but I'm not sure what?

Here is all of the code.

if ( file_exists(  __DIR__ . '/cmb2/init.php' ) ) {
  require_once  __DIR__ . '/cmb2/init.php';

function cmb2_hide_if_no_cats ( $field ) { if ( ! has_tag ( 'cats', $field->object_id ) ) { return false; } return true; }

add_action ( 'cmb2_init', 'cmb2_add_metabox' );
function cmb2_add_metabox() {
    $prefix = 'cmb2';
    $cmb = new_cmb2_box ( array (
        'id'           => $prefix . 'title2',
        'title'        => __('title2', 'cmb2'),
        'context'      => 'normal',
        'priority'     => 'default', ));
    $cmb->add_field ( array (
        'name' => __('title2', 'cmb2'),
        'id' => $prefix . 'title2',
        'type' => 'text',
        'default' => 'title', )); }}

Took me a few minutes to spot this, but it's plural object_types, not singular object_type

function cmb2_hide_if_no_cats( $field ) {
    if ( ! has_tag( 'cats', $field->object_id ) ) {
        return false;
    }

    return true;
}

add_action( 'cmb2_init', 'cmb2_add_metabox' );
function cmb2_add_metabox() {
    $prefix = 'cmb2';
    $cmb    = new_cmb2_box( array(
        'id'       => $prefix . 'title2',
        'title'    => __( 'title2', 'cmb2' ),
        'context'  => 'normal',
        'priority' => 'default',
        'object_types' => array( 'movie' )
    ) );
    $cmb->add_field( array(
        'name'    => __( 'title2', 'cmb2' ),
        'id'      => $prefix . 'title2',
        'type'    => 'text',
        'default' => 'title',
    ) );
}

screen shot 2016-10-03 at 11 37 51 am

Obviously change the "movie" post type I used for my dev site to "review".

Your paste above has an extra }, just so you know. Copy/paste mine here and you should be covered on that topic.

I still don't understand :( This is what I have in my functions file, with little else except a little bit in my header.

if ( file_exists(  __DIR__ . '/cmb2/init.php' ) ) {
  require_once  __DIR__ . '/cmb2/init.php';

function cmb2_hide_if_no_cats( $field ) {
    if ( ! has_tag( 'cats', $field->object_id ) ) {
        return false;
    }

    return true;
}

add_action( 'cmb2_init', 'cmb2_add_metabox' );
function cmb2_add_metabox() {
    $prefix = 'cmb2';
    $cmb    = new_cmb2_box( array(
        'id'       => $prefix . 'title2',
        'title'    => __( 'title2', 'cmb2' ),
        'context'  => 'normal',
        'priority' => 'default',
        'object_types' => array( 'Blog Tours', 'Reviews' )
    ) );
    $cmb->add_field( array(
        'name'    => __( 'title2', 'cmb2' ),
        'id'      => $prefix . 'title2',
        'type'    => 'text',
        'default' => 'title',
    ) );
}

} ?>

and it's still not showing up :(

The custom post type slug is called reviews and the name is Reviews. I've tried it both ways and neither worked. I've tried it without the Blog Tours added on too and that didn't work either. I just don't get it.

The last }, right before the closing ?> should probably go above your first function declaration. Declaring the functions isn't going to break anything if that file doesn't exist.

Use the post type slugs, not their plural labels.

Everything else looks fine at the moment other than those two points.

Ah ha! Apparently I needed the entire directory instead of just "DIR " -- I don't think it was picking it up for some reason. That worked when I substituted that for the actual directory path.

Awesome to hear you have made progress and is on the way to making some awesome metaboxes.

Going to close this as a non-issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gorirrajoe picture gorirrajoe  路  4Comments

stabilimenta picture stabilimenta  路  8Comments

yeahsmaggy picture yeahsmaggy  路  9Comments

GaryJones picture GaryJones  路  3Comments

angelorocha picture angelorocha  路  6Comments