CMB2 and custom post type?
-
Just trying to use this with a custom post type but it doesn’t seem to display the value for a custom box again once entered and the post has been saved. Does it not work for custom post types?
-
It should.
My main question is if you kept the example prefix,
_cmb2_
or something similar to it, specifically that leading underscore. The underscore prevents the meta value from showing in the “Custom Fields” metabox, but the meta data is still saved to the post. If you crack open phpmyadmin and check the wp_postmeta table for all of the meta on that post ID, chances are you’ll see your saved values.Hi Michael,
same issue here.
If I use CMB2 custom fields with pages it works! (in the example the prefix is _cmb2_)
mysql> select count(meta_id) from wp_postmeta where meta_key like ‘_cmb2_%’;
+—————-+
| count(meta_id) |
+—————-+
| 35 |
+—————-+
1 row in set (0.00 sec)If I use CMB2 with a custom post type, the value entered into the custom fields are not saved! (in the example the prefix is _corso_)
mysql> select count(meta_id) from wp_postmeta where meta_key like ‘_corso_%’;
+—————-+
| count(meta_id) |
+—————-+
| 0 |
+—————-+
1 row in set (0.00 sec)After this I used your example.
With pages it works.
With my own CPT it doesn’t.What could it be?
Do you have any example with CPT?Thank you!
Just to be certain, you’re both using the post type SLUG with the “object_type” parameter, correct? Also, are you using the “object_type” parameter instead of “pages” from CMB1?
I have it working, even with the _corso_ prefix. For an example of the code I’m using:
Hi,
Thanks for your reply, yes, I am using it correctly, pretty much like your code, see below
function cmb2_metaboxes( array $meta_boxes ) { $prefix = '_cmb2_'; $meta_boxes['my_metabox'] = array( 'id' => 'route_details_metabox', 'title' => __( 'Route details', 'cmb2' ), 'object_types' => array( 'myroutes',), 'context' => 'normal', 'priority' => 'high', 'show_names' => true, 'fields' => array( array( 'name' => __( 'Route date', 'cmb2' ), 'desc' => __( 'date for the route', 'cmb2' ), 'id' => $prefix . 'test_textdate', 'type' => 'text_date', 'date_format' => 'd/m/Y' ), array( 'name' => __( 'Start time', 'cmb2' ), 'desc' => __( '', 'cmb2' ), 'id' => $prefix . 'test_time', 'type' => 'text_time', ) ), ); return $meta_boxes; }
Any ideas?
I thought the extra comma in the object_types array would potentially be an issue, but i saw it in my gist from last night too, and it’s still working, so that’s out.
Otherwise, I can’t see anything wrong with the code above, and have to wonder if you’re perhaps accidentally looking at the post meta for the wrong rows in the db. Another way to help test, remove the
_
in front of the prefix, as that hides it from the Custom Fields metabox. Try it without and see if they show up there after saving.Hi,
thought about the comma but you have that in your example, too ??
No, definitely looking at the right rows as I can see it changing in the database but just not showing up in the editor screen. Have tried removing the _ , no joy either ??
Is this a dev site, where you could disable all other plugins for testing purposes? Do you perhaps have a copy of CMB1 installed and still getting used by any chance?
Yes, it is a dev site. Have disabled all plugins and never had a copy of CMB1. Still no joy ?? Data gets written into the post mneta table but when loading the custom post again, it’s not loading in ??
Hrm.
Is this a publicly accessible dev site if you know the url? or is it something you’re working on locally only at the moment? At this point I’m curious what’s going on and if I could get a WP user and possibly FTP/phpmyadmin access if necessary. Would you be willing to let me in to that much for long enough to debug what’s going on? Because nothing is making sense for why it’s failing at the moment.
Solved.
I created my CPT using https://generatewp.com/post-type/.
The generated code has the name of the CPT with the first letter capitalized, while the name of the SLUG is the same name, but with the first letter lowercase!
The interface generated with CMB2 appeared on the administration page (add / edit) of the CPT, but the data entered in the various fields were not saved.
WRONG:
'object_types' => array( 'Corso', ), // Post type
CORRECT:
'object_types' => array( 'corso', ), // Post type
Thank you Michael!
Have a nice day.Sorry, not solved for me, not an issue of case. Michael, I can arrange for you to have access but there is no pm option here, so trying to get in touch with you via g+
mouseuser, just email me the necessary information to [email protected], it’ll be the quickest way to get ahold of me.
fmarasco, yes, the slug as opposed to anything with the labels, is what you need to specify with CMB2 object_type param.
Either of you still having lingering issues? or is this thread good to mark resolved?
thanks for following up – it’s OK, I have resolved the issues although the issue with date display is still there see other thread
- The topic ‘CMB2 and custom post type?’ is closed to new replies.