Custom fields not showing
- This topic has 5 replies, 2 voices, and was last updated 3 days, 18 hours ago by
Jai Ivarsson.
-
AuthorPosts
-
June 30, 2025 at 5:16 PM #48518
Jai Ivarsson
ParticipantI have created numerous plugins with Meta Box and extensions bundled in using composer without problems but for some reason, my latest plugin isn't working. The custom post type is being created and I can var_dump to see that the add_filter for adding the fields is being called correctly but the custom fields just aren't showing on the custom post edit page. I even put one of my other plugins in instead to rule out something specific with this site and it didn't work.
This is my Custom post class. Anyone see something stupid I'm doing?
class RentalPostType extends Base { public function register() { add_action('init', [$this, 'register_rental_post_type']); add_filter('rwmb_meta_boxes', [$this, 'rental_fields']); } public function register_rental_post_type() { $labels = [ 'name' => esc_html__('Rentals', $this->text_domain), 'singular_name' => esc_html__('Rental', $this->text_domain), 'add_new' => esc_html__('Add New', $this->text_domain), 'add_new_item' => esc_html__('Add New Rental', $this->text_domain), 'edit_item' => esc_html__('Edit Rental', $this->text_domain), 'new_item' => esc_html__('New Rental', $this->text_domain), 'view_item' => esc_html__('View Rental', $this->text_domain), 'view_items' => esc_html__('View Rentals', $this->text_domain), 'search_items' => esc_html__('Search Rentals', $this->text_domain), 'not_found' => esc_html__('No rentals found.', $this->text_domain), 'not_found_in_trash' => esc_html__('No rentals found in Trash.', $this->text_domain), 'parent_item_colon' => esc_html__('Parent Rental:', $this->text_domain), 'all_items' => esc_html__('Rentals', $this->text_domain), 'archives' => esc_html__('Rental Archives', $this->text_domain), 'attributes' => esc_html__('Rental Attributes', $this->text_domain), 'insert_into_item' => esc_html__('Insert into rental', $this->text_domain), 'uploaded_to_this_item' => esc_html__('Uploaded to this rental', $this->text_domain), 'featured_image' => esc_html__('Featured image', $this->text_domain), 'set_featured_image' => esc_html__('Set featured image', $this->text_domain), 'remove_featured_image' => esc_html__('Remove featured image', $this->text_domain), 'use_featured_image' => esc_html__('Use as featured image', $this->text_domain), 'menu_name' => esc_html__('Rentals', $this->text_domain), 'filter_items_list' => esc_html__('Filter rentals list', $this->text_domain), 'filter_by_date' => esc_html__('', $this->text_domain), 'items_list_navigation' => esc_html__('Rentals list navigation', $this->text_domain), 'items_list' => esc_html__('Rentals list', $this->text_domain), 'item_published' => esc_html__('Rental published.', $this->text_domain), 'item_published_privately' => esc_html__('Rental published privately.', $this->text_domain), 'item_reverted_to_draft' => esc_html__('Rental reverted to draft.', $this->text_domain), 'item_scheduled' => esc_html__('Rental scheduled.', $this->text_domain), 'item_updated' => esc_html__('Rental updated.', $this->text_domain), ]; $args = [ 'label' => esc_html__('Rentals', $this->text_domain), 'labels' => $labels, 'description' => '', 'public' => true, 'hierarchical' => false, 'exclude_from_search' => false, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'show_in_rest' => true, 'query_var' => true, 'can_export' => false, 'delete_with_user' => false, '_slug_changed' => true, 'has_archive' => true, 'rest_base' => '', 'show_in_menu' => true, 'menu_position' => '', 'menu_icon' => 'dashicons-location', 'capability_type' => 'post', // 'capabilities' => [ // 'create_posts' => 'do_not_allow', // Removes 'Add New' button // ], 'supports' => ['title', 'thumbnail'], 'taxonomies' => ['sd-region', 'sd-city', 'sd-suburb'], 'rewrite' => [ 'with_front' => false, ], ]; register_post_type('sd-rental', $args); } public function rental_fields($meta_boxes) { $meta_boxes[] = [ 'title' => __('Rental Fields', $this->text_domain), 'id' => $this->text_domain . '-rental-fields', 'post_types' => ['sd-rental'], 'closed' => false, 'revision' => false, 'fields' => [ [ 'name' => __('Display Address', $this->text_domain), 'id' => $this->prefix . 'display_address', 'type' => 'text', 'hide_from_rest' => false, 'hide_from_front' => false, 'limit_type' => 'character', ], ] ]; return $meta_boxes; } }
June 30, 2025 at 10:37 PM #48519Peter
ModeratorHello Jai,
Thanks for reaching out.
There could be an issue with the priority of loading the Meta Box plugin. If I add your code to the file functions.php in a theme folder, the CPT and custom fields are registered as well. Please check this screenshot https://ibb.co/v4LT2SF2
I think that when you plugin/custom code is executed before Meta Box plugin and filter hook
rwmb_meta_boxes
so the custom field is not registered to display on the editing post.July 1, 2025 at 2:56 AM #48520Jai Ivarsson
ParticipantThanks Peter. I have been suspecting load order as the cause but can't see how to fix it...
July 1, 2025 at 3:45 AM #48521Jai Ivarsson
ParticipantI tried loading the fields in the functions.php and it worked.
I added some debugging to try and understand what is happening.this in the plugin class that registers the post type and fields:
error_log('Admin Init - MetaBox Debug:'); error_log('RWMB_VER: ' . (defined('RWMB_VER') ? RWMB_VER : 'not defined')); error_log('MetaBox Class exists: ' . (class_exists('RWMB_Core') ? 'yes' : 'no'));
All comes back as expected in the logs:
Admin Init - MetaBox Debug:
RWMB_VER: 5.10.10
MetaBox Class exists: yesI added this after the registration of everything and confirmed that nothing is being registered:
$meta_box_registry = rwmb_get_registry('meta_box'); $field_registry = rwmb_get_registry('field'); var_dump($meta_box_registry->all()); var_dump($field_registry);
both dumps being empty
July 1, 2025 at 11:19 PM #48522Peter
ModeratorHello,
So we can see that there are no issues with the Meta Box plugin itself. You should recheck your custom code and investigate the issue.
If you are not able to find it, we offer a customization service with an extra fee. Please contact us here https://metabox.io/contact/ for more details.July 6, 2025 at 10:22 AM #48537Jai Ivarsson
ParticipantOk, leaving this last comment, even though it shows how dumb I was and how long it took be to find it 🙁
When defining the fields, on a couple of taxonomy custom fields I was doing this:
public function suburb_fields($meta_boxes) { $meta_boxes = [ 'title' => 'Suburb Taxonomy Fields', 'id' => $this->text_domain . '-sd-suburb-fields', 'taxonomies' => ['sd-suburb'], 'fields' => [ [ 'name' => __('City', $this->text_domain), 'id' => $this->prefix . 'city', 'type' => 'taxonomy', 'taxonomy' => ['sd-city'], 'field_type' => 'select_advanced', 'add_new' => false, 'remove_default' => false, ], ] ]; return $meta_boxes; }
Instead of this:
public function suburb_fields($meta_boxes) { $meta_boxes[] = [ 'title' => 'Suburb Taxonomy Fields', 'id' => $this->text_domain . '-sd-suburb-fields', 'taxonomies' => ['sd-suburb'], 'fields' => [ [ 'name' => __('City', $this->text_domain), 'id' => $this->prefix . 'city', 'type' => 'taxonomy', 'taxonomy' => ['sd-city'], 'field_type' => 'select_advanced', 'add_new' => false, 'remove_default' => false, ], ] ]; return $meta_boxes; }
Note the
$meta_boxes = ...
instead of$meta_boxes[] = ...
This stupid mistake on two taxonomy definitions was breaking everything, settings screens, custom post fields and of course taxonomy fields. -
AuthorPosts
- You must be logged in to reply to this topic.