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

Get or set values in post meta

In the end, I’ve managed to do in this way

(function( plugins, editPost, element, components, data, compose ) {

    const el = element.createElement;

    const { Fragment } = element;
    const { registerPlugin } = plugins;
    const { PluginSidebar, PluginSidebarMoreMenuItem } = editPost;
    const { PanelBody, ColorPicker } = components;
    const { withSelect, withDispatch } = data;

    const MetaThemeControl = compose.compose(
        withDispatch( function( dispatch, props ) {
            return {
                setMetaValue: function( metaValue ) {
                    dispatch( 'core/editor' ).editPost(
                        { meta: { [ props.metaKey ]: metaValue } }
                    );
                }
            }
        } ),
        withSelect( function( select, props ) {
            return {
                metaValue: select( 'core/editor' ).getEditedPostAttribute( 'meta' )[ props.metaKey ],
            }
        } ) )( function( props ) {
            return el( ColorPicker, {
                label: props.title,
                value: props.metaValue,
                onChangeComplete: function( content ) {
                    props.setMetaValue( content.hex );
                },
            });
        }
    );

    registerPlugin( 'b-theme-sidebar', {
        render: function() {
            return el( Fragment, {},
                el( PluginSidebarMoreMenuItem,
                    {
                        target: 'b-theme-sidebar',
                        icon: 'admin-appearance',
                    },
                    'B Theme'
                ),
                el( PluginSidebar,
                    {
                        name: "b-theme-sidebar",
                        title: "B Theme",
                        icon: 'admin-appearance',
                    },
                    el( PanelBody, {},
                        // primary color
                        el( MetaThemeControl,
                            {
                                metaKey: 'primary_theme',
                                title : 'Primary Theme',
                            }
                        ),
                        // secondary color
                        el( MetaThemeControl,
                            {
                                metaKey: 'secondary_theme',
                                title : 'Secondary Theme',
                            }
                        ),
                    )
                )
            );
        }
    } );
} )(
    window.wp.plugins,
    window.wp.editPost,
    window.wp.element,
    window.wp.components,
    window.wp.data,
    window.wp.compose
);

But I would like to find a solution for my first attempt, cause is much more readable.

EDIT: a tutorial that would resolve the first case:

https://css-tricks.com/managing-wordpress-metadata-in-gutenberg-using-a-sidebar-plugin/

Related Posts:

  1. post formats – how to switch meta boxes when changing format?
  2. Adding an assisting editor box to Post page
  3. Metabox Data not being saved [closed]
  4. WordPress widget new instance creates content duplicates
  5. Options to get my custom post type metadata via the WordPress API
  6. Add box with custom per-page properties
  7. Adding a meta box to determine the sidebar [closed]
  8. add post meta front end edit
  9. Custom sidebars – get_sidebar() problems
  10. I can not display meta value in extras.php and template-tags.php
  11. Add post meta data date to event
  12. Check if any meta on the post has value then display content
  13. I created a Custom Meta Box but it is not displaying the value on my post page
  14. update_post_meta() throws Uncaught error: Cannot create duplicate attribute
  15. Remove action of an external plugin after checking if custom post meta is set
  16. How we insert values into database using metabox WordPress?
  17. Post meta box data not saving
  18. Hide individual page title using checkbox in custom meta box?
  19. Add “upload media” button in meta box field
  20. Get list of all registered sidebars
  21. Execute action after post is saved with all related post_meta records (data)
  22. How get post id from meta value
  23. Safe to delete blank postmeta?
  24. get_post_meta returning empty string when data shows in the database
  25. publish_post action hook doesn’t give post_meta_data
  26. How to access the post meta of a post that has just been published?
  27. How to get all term meta for a taxonomy – getting term_meta for taxonomy
  28. Should I sanitize custom post meta if it is going to be escaped later?
  29. Query between two meta values?
  30. List posts under meta_value heading
  31. get_post_meta doesn’t work
  32. loop through all meta keys with get_post_meta
  33. How to add multiple featured image in meta box in post editor?
  34. duplicate sidebar
  35. Saving html into postmeta without stripping tags – safe?
  36. How to show Published date and/or Modified date
  37. update_post_meta and get_post_meta not working
  38. wp_update_user isn’t instantly?
  39. Repeatable custom meta fields
  40. special characters after saving draft interpreted as �
  41. How to use update_post_meta inside wp_trash_post
  42. “update_post_meta” not working in “wp_insert_post_data” hook
  43. Display All Custom Post Fields and Values, Unless Empty
  44. How to display custom sidebar in wordpress 5.5.2
  45. How to create a meta_query to get all posts with a specific meta data?
  46. Why is my Custom Meta Box Field Inputs NOT saving?
  47. hide posts with specific meta data from admin page
  48. Post MetaTable Overload
  49. How do you List all Sidebars in a Metabox
  50. How to increase maximum number of sidebars support?
  51. Get a row from a separate table by matching a posts meta_key to a tables ID column
  52. Reset/delete post views meta/custom field after X hours or minutes or seconds
  53. Date format – Meta Box plugin
  54. How to Echo Metadata Value in Currency Format
  55. Have lots of meta for posts, is it better to get at all at once or each individually
  56. How to use media upload on metabox post page without breaking TinyMCE?
  57. Query posts WITHOUT a custom meta field
  58. Let’s Create Custom Field Template Documentation
  59. get_post_meta not working when variable used for post ID
  60. Update post meta dynamically
  61. Need a SQL query to update meta_key=’_price’ with value in meta_key=’_regular_price’
  62. Display current user’s custom post meta in sidebar
  63. Register_Sidebar overwriting itself and doesn’t exist in global $wp_registered_sidebars;
  64. add_post_meta doesn’t work
  65. Can I save post meta programatically without setting metaboxes?
  66. Meta box data is saved but NOT displayed in the meta box text field. Why?
  67. How to get single value from get_post_meta() array of values?
  68. Meta box values are displayed on Custom Fields list. Is it possible to hide them?
  69. wordpress update multiple posts post meta
  70. get_template_part() isn’t loading author information
  71. Is there a way combine posts meta_name?
  72. Why is my custom sidebar always open on the widgets screen in Admin?
  73. Custom Meta Data is not being saved
  74. Count Post and Page Views based on meta_value Using Shortcode in Dashboard Widget
  75. Removing Malware
  76. After updating the custom post type, metafields disappear from the post.php edit menu, how do I fix it?
  77. URL from get_post_meta() is broken my URL
  78. WordPress Blocks, setAttributes not saving
  79. update_post_meta doesn’t work
  80. how to query posts using value in meta post array
  81. delete post meta data in array WordPress
  82. Why doesn’t wp_oembed_get() for the video post format not work?
  83. Custom Meta Fields that are Echo’d are removed on post update?
  84. Save, update, get and sanitize post meta as HTML not plain
  85. How to add dynamic content in title and meta description in wordpress theme for homepage, post page, category, tag and pages
  86. How to call get_post()?
  87. WordPress AJAX load post metadata in modal
  88. Edit post meta direct from post.php?
  89. Looking for suggestions on creating simple database (Help!!)
  90. Can’t save drop down select date in meta boxes
  91. Posts with no meta field do not appear when sorting by meta field
  92. How do I delete element from a serialized array upon deletion of a post?
  93. Get meta information from post parent
  94. Unique widget id in sidebar
  95. Use a Variable in update_post_meta as the $meta_key
  96. query with custom field
  97. Using Form to alter PHP variable [closed]
  98. My meta box don’t want to save value
  99. Update post meta array – add new, single value
  100. How to update post view count?
Categories post-meta Tags metabox, post-meta, register-sidebar, sidebar
How to remove private posts from RSS feeds?
Custom filter from post meta

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