I'd like a custom field, that can only be assigned to one post at a time. Ideally directly from the Dashboard.
Let's say I have a custom field featured post
on /post-123
with value true
.
If I assign featured post: true
to post-111
, the value of post-123
's featured post
-custom field needs to get the value false
or deleted entirely.
This means that my custom field can only be assigned to one post.
OR
A custom field with a specified value can only exist once.
Is there a plugin that can do it? Or is it possible with the WordPress plugin Types?
The custom field saving/updating/removing is done in the action hook
save_post
.Before updating the current post meta data, we query all other posts that may contain the same meta and remove it case positive.
Basically:
And here, a full working example using a meta box based in Add a checkbox to post screen that adds a class to the title: