Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Custom Meta Box not Saving in Posts with Gutenberg Editor

So, it simply turns out that the method I usually use to save post meta doesn’t work in this context, even though it does in when using the classic editor. I don’t know why this is, but I did find another article (https://www.codeinwp.com/blog/make-plugin-compatible-with-gutenberg-sidebar-api/) and there was a line in there that eventually led me to a solution. Additionally the entire, mycpt_register_post_meta() function doesn’t appear to be necessary at all if you’re saving data in this manner. For the clarification, I didn’t actually show my usual saving method in my question because it wasn’t working and I’d long since abandoned trying to use it in this particular instance – so I didn’t include it. (If anyone wants to see it I can add it.)

function mycpt_add_cpt_selection() {
    $screens = ['page', 'post'];
    foreach( $screens as $screen ) {
        add_meta_box(
            'mycpt_assignment',
            'Assign Quick Links',
            'mycpt_assign_metabox',
            $screen,
            'side',
            'low',
             //not even sure that this array is necessary
             array(
                '__block_editor_compatible_meta_box' => true,
                '__back_compat_meta_box'             => false,
            )
        );
    }
}
add_action( 'add_meta_boxes', 'mycpt_add_cpt_selection' );
function mycpt_assign_metabox( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'mycpt_cpt_nonce' );
    $mycpt_appended_cpts = get_post_meta( $post->ID, 'mycpt_cpts', false ); ?>
    <select id="mycpt_cpts" name="mycpt_cpts[]" multiple="multiple" style="width:90%;">
    <?php
    if( $mycpt_appended_cpts ) {
        foreach( $mycpt_appended_cpts as $cpt_ids ) {
            foreach( $cpt_ids as $cpt_id ) {
                $cpt_title = get_the_title( $cpt_id );
                $cpt_title = ( mb_strlen( $cpt_title ) > 50 ) ? mb_substr( $cpt_title, 0, 49 ) . '...' : $cpt_title;
                echo '<option value="' . $cpt_id . '" selected="selected">' . $cpt_title . '</option>';
            }
        }
    }
    ?>
    </select>
    <small style="display:block;font-size:0.8em;width:95%;">
        Instructions
    </small>
<?php }
function mycpt_assign_save_postdata( $post_id ) {
    if( isset( $_POST['mycpt_cpts'] ) ) {
        update_post_meta( $post_id, 'mycpt_cpts', $_POST['mycpt_cpts'] );
    }
}
add_action( 'save_post', 'mycpt_assign_save_postdata' );

So everything works almost exactly the way I want it. The only oddity that still perplexes me is when retrieving the post_meta it’s actually putting an array into another array. It’s creating a multi-dimensional array, which isn’t what happens when I use the same methods in other areas.

As an example, in the database, when I’ve assigned four of my CPT posts to a WP post – it’s saved as the following:

a:2:{i:0;s:2:"65";i:1;s:2:"66";}

Which would be:

array(
  0 => "65",
  1 => "66"
)

However, when I retrieve using get_post_meta(), what I get back is the following:

array(
  0 => array(
    0 => "65",
    1 => "66" 
  ),
)

You can see from select output that I have 2 foreach loops in there, so I’ve got a workaround, but I’m still going to figure out how to get it to return just a single array.

Hope someone else finds this useful.

Related Posts:

  1. Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
  2. How to save a ToggleControl value in a meta field?
  3. Looping inside block return
  4. Update post meta custom field using block editor
  5. WordPress Blocks, setAttributes not saving
  6. Can’t set custom meta fields for a post
  7. Custom meta POST request fired twice when updating a post in Gutenberg
  8. Block Editor – Meta values not saved, meta changes to empty array on update
  9. What is the best way to get a different post’s custom field/postmeta with js?
  10. Can I exclude a post by meta key using pre_get_posts function?
  11. Custom post meta field effect on the performance on the post
  12. How to get custom post meta using REST API
  13. Difference between meta keys with _ and without _ [duplicate]
  14. Orderby meta_value only returns posts that have existing meta_key
  15. What is the index [0] for on post meta fields?
  16. What is “meta_input” parameter in wp_insert_post() used for?
  17. How to enable revisions for post meta data?
  18. The “_encloseme” Meta-Key Conundrum
  19. Best way to programmatically remove a category/term from a post
  20. Gutenberg add a custom metabox to default blocks
  21. Using get_post_meta with new_to_publish
  22. Add metabox to document tab in gutenberg
  23. Custom field metabox not showing in back-end
  24. So much data in postmeta
  25. Can I count the number of users matching a value in a multiple value key?
  26. When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?
  27. How to hide meta box values from custom fields list?
  28. Auto sort the wp-admin post list by a meta key
  29. get_post_meta() unserialize issue – returns boolean(false)
  30. What is the advantage of the wp_options design pattern?
  31. Storing meta fields multiple times OR once with multi dimensional array?
  32. Allow user to create instances of custom field
  33. extend Meta Box / Document Panel
  34. display specific custom fields
  35. Is there a hook / action that is triggered when adding or removing a post thumbnail?
  36. Meta keywords and descriptions plugin for manually editing meta for each page/post
  37. getEntityRecord without knowing the post type
  38. passing argument to get_template_part() or a better way to code
  39. Is it possible to store arrays in a custom field?
  40. Get updated meta data after save_post hook
  41. Multiple meta values for same meta_key adding on “Preview Changes” hit but not on saving or updating post
  42. Save HTML formatted data to post meta using add_post_meta()
  43. importing data from non-wordpress mysql db
  44. Gutenberg Custom Block
  45. Create meta boxes that don’t show in custom fields
  46. Transients vs CRON +Custom Fields: Caching Data Per Post
  47. Unable to save datetime custom meta field using update_post_meta() function
  48. Up/Down voting system for WordPress
  49. post meta data clearing on autosave
  50. Create custom field on post draft or publish?
  51. Display info from custom fields in all images’ HTML
  52. Ordering posts by anniversary using only day and month
  53. get_post_meta fields don’t show up on posts page
  54. Update meta values with AJAX
  55. How to break meta values into different items and avoid duplicates?
  56. copy attachments to another post type and change attachment url
  57. Cannot edit post meta fields with rest API
  58. ajax delete value from custom field array
  59. Save attachment custom fields on front end
  60. How to use pagination with get_post_meta
  61. Copying Custom Meta Values from existing post to a duplicate post
  62. Add a post meta key and value only if it does not exist on the post
  63. Move value of one custom field to another
  64. Order posts according to user defined order for meta values?
  65. Displaying posts with only upcoming dates according their custom field date value
  66. Custom fields to save multiple values
  67. Custom fields: In what order are they saved into the DB?
  68. Function to change meta value in database for each post
  69. Get a post_id where meta_value equals something in a serialized meta_value field
  70. Get aggregate list of all custom fields for entire blog
  71. Unable to show ACF’s Image Custom Field properly in Genesis Framework [closed]
  72. Custom field value based on other custom field values
  73. wp_handle_upload error “Specified file failed upload test” but still creates attachment?
  74. Which is best in the following scenario : post_meta vs custom table vs parent/child posts
  75. Saving custom image meta fields
  76. Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
  77. How to display Meta Field Value?
  78. MySQL query to set wp_postmeta using term_taxonomy_id value
  79. How to Validate Post Meta type/extension (Video File Image File etc)
  80. Get all meta keys assigned to a post type
  81. using multiple meta_key and meta_value in query_posts
  82. Adding custom fields (post meta) before/during wp_insert_post()
  83. Combine multiple custom field values into single value
  84. How can I sort homepage by a meta value?
  85. Get specific custom field keys from a post and put into an array
  86. How do I use wp_query for WordPress search?
  87. Nav Menu – Add class based on meta keys
  88. How to query posts with certain custom meta data, and output Post data
  89. MySQL Query that looks for post with Custom Field, then changes Category
  90. ACF: How to get the full field name (meta_key) by a field key?
  91. How to update a meta field of type array in Gutenberg
  92. How to wrap meta values seperated by comma in ? [closed]
  93. Bulk remove post meta
  94. post meta getting deleted on save
  95. How to create html block to display extra information on woocommerce single product page
  96. Can’t get post ID using wp_insert_post_data
  97. Unique Post Meta Values
  98. Create Multiple File Upload Metabox in WordPress
  99. filtering custom post types via meta data drop down
  100. How to add a new meta key and assign timestamp to posts
Categories custom-field Tags block-editor, custom-field, post-meta
PHP Parse error: syntax error, unexpected end of file, expecting elseif (T_ELSEIF) or else (T_ELSE) or endif (T_ENDIF) on line 230 [closed]
Uncheck the box “Send User Notification” by default on new-user.php

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress