When you do this: '_related_posts': [post.id]
, you’re actually overriding the entire array or the meta value, so to instead append an item to the existing array, you can use the spread (...
) syntax like so: '_related_posts': [ ...meta._related_posts, post.id ]
.
But you may not want to update the meta if the selected post ID is already in the current meta value, i.e.
// Add the post (ID) if it's not already in the list.
if ( meta._related_posts.indexOf( post.id ) < 0 ) {
setMeta( { ...meta, '_related_posts': [ ...meta._related_posts, post.id ] } );
}
That’s just a suggestion (to avoid duplicate values), though.
And BTW, for property names that do not contain characters that are not alphanumeric or underscore (_
), it’s not mandatory to enclose the property name in quotes. So for example in your case, _related_posts:
(as opposed to having the quotes: '_related_posts':
) would be fine. 🙂
Related Posts:
- getEntityRecord without knowing the post type
- How to save a ToggleControl value in a meta field?
- Add new image block, set default class name and update it using Javascript
- Validating Custom Meta Box Values & Required Fields
- Is there a way to set default custom fields when creating a post?
- Set default image link target in Gutenberg image block
- Is there any action filter/hook for validating a custom field before publishing the post?
- Add subtitle to Woocommerce product title
- How to use wp.hooks.addAction() in React JS/Gutenberg?
- Gutenberg add a custom metabox to default blocks
- Using TinyMce with textareas in meta boxes on custom post types
- Add metabox to document tab in gutenberg
- Extend core block in Gutenberg
- Unable to get Preview of Uploaded image within a Custom Meta box
- Hooking a callback into the code within a Gutenberg block
- extend Meta Box / Document Panel
- Get updated meta data after save_post hook
- Gutenberg Custom Block
- When using the block.getSaveElement hook can you output different markup based on whether or not you are in the edit view or the live page view?
- Check before publishing, if already exist post with current custom field value
- HTML / Javascript in custom field textarea?
- post meta data clearing on autosave
- Store JSON in a custom field
- Display info from custom fields in all images’ HTML
- Using custom fields in a filter hook
- What do the args for Gutenberg subpackage “hooks” function “doAction” mean?
- Update meta values with AJAX
- Why does this Quick Edit checkbox save the value only when unchecked?
- Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
- Use useSelect/useDispatch instead of withSelect/withDispatch
- How to use wp.hooks.addAction() in React JS/Gutenberg?
- Registration and Profile custom field
- Custom Meta Box not Saving in Posts with Gutenberg Editor
- Print custom field in Query Loop block
- Unsaved changes in metabox inputs not detected in the block editor
- Possible to add a meta field for css attribute to the editor Advanced panel?
- Send email with custom fields after new draft is saved or new post published
- Manipulate javascript code from custom made admin page menu
- jQuery – Automatic event trigger does not **really** check my form
- Populate Javascript code with value from WordPress Custom Field?
- Hook after meta created
- useSelect second parameter
- Can’t save custom field on registration page
- Get terms of a taxonomy using useSelect
- Looping inside block return
- Update post meta custom field using block editor
- Best Way to get facebook share count and update using wp_schedule_event or any other method
- No result update a custom field using wp_schedule_event
- Using Custom Fields to Trigger Specific Actions on Saving a Post?
- Meta field bulk editing no longer working in WP 6.0 [closed]
- Type character in name and make it invisble
- how to enable custom fields in the Gutenberg post editor
- How to set up page fields with gutenberg instead of ACF
- Refresh Gutenberg with JS for it show updated post
- Trigger function on Remove block or add new block in Gutenberg JavaScript
- Gutenberg add a custom metabox to default blocks
- Making custom meta box required (with error message if not filled in) on Gutenberg
- Issue migrating a checkbox-type meta field to the block editor
- WordPress Blocks, setAttributes not saving
- Why isn’t update_post_meta saving?
- Can’t set custom meta fields for a post
- Custom meta POST request fired twice when updating a post in Gutenberg
- change attachment custom field onChange event
- Custom field in external Javascript
- Change event firing in wordpress
- Incorrect data after modifying post using backlink from wp_die
- How can I duplicate “Add Custom Field” with just JavaScript?
- How to automatically create a custom field when a post is published?
- Add custom fields to specific menus
- Custom Fields after update to WordPress 6
- How can I copy value from dropdown once I select it to text area as text?
- Set new url from custom post field
- Is there a way to read JSON data inside Custom Fields without editing PHP? [closed]
- Modal pop-up HTML code works on other sites or HTML viewers but not in the custom HTML block within a wordpress page?
- How to add a custom field in the advanced menu properties?
- Adding Custom Field to Taxonomy Input :Panel
- How to filter or search the posts using postmeta tables custom meta fields with wordpress REST API
- Custom bulk action for media gallery items
- Localized Date Format for Custom Field
- Check if the value of a field has changed on save_post
- Saving custom image meta fields
- Query post order by post and desc not working
- If category then echo a custom fields
- Auto Populate Custom Field with Complex Value That Increase by One?
- Display custom field if it exists and has a specific value [closed]
- Is there any way to get all meta and standard columns for any WordPress object after searching based on meta key and value
- How to add fetchpriority parameter to images in the media library?
- Block Editor: Reset Reusable Blocks to last saved record
- Editable custom field in WP Admin Dashboard
- Customizing user data
- Iterate through users and display users meta info at front-page
- Custom field image to Featured Image
- Show array of meta_value in Edit Post Coloum
- Working with multiple values and metaboxes
- Unable to select image with custom field
- search based on custom field
- Update block once an API request returns with a value
- Display taxonomy loop using custom field data
- Custom fields / meta box output
- How to add google map to custom field