[Resolved] Deactivate update_post_meta for some custom fields in some Post Types
This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
I want to disable update_post_meta() for some custom fields and post types. e.g. If Any Admin on my Website want to update some custom post type, than must be all other custom post fields updated, but some custom post field e.g." wpcf-overdue" not. How can I do this?
Hello. Thank you for contacting the Toolset support.
I understand your concern but there is no such feature available to hide single post field at admin section conditionally by design.
What I suggest:
1)
Create custom fields groups and add fields to this group which you want to hide I mean which you want to display conditionally.
2)
Please add following code to your current theme's funcitons.php file.
function custom_remove_post_widgets() {
global $post_type;
global $post;
global $current_user;
if(in_array('administrator',(array)$current_user->roles)){
remove_meta_box( 'wpcf-group-{YOUR-POST-FIELDGROUP-SLUG}', $post_type,'normal');
}
}
add_action('admin_head', 'custom_remove_post_widgets' );
Where:
- Replace '{YOUR-POST-FIELDGROUP-SLUG}' with your original custom fields group slug.
I hope above solution will help you to resolve your issue.
1. Thanks, you code works very well. This is solution, if Admin works on the page of some post type e.g. "Room" , that have this field e.g."Height". There is with your code removed and he cannot update it. This is perfect. But if Admin works on a page "Home", that is parent of "Room", than Admin can though see this custom field as field of child Post Type "Room". Can I remove also there these fields "Height"?
2. Must I add with Custom Types some custom field to use function add_post_meta() or update_post_meta() ? I added did add_post_meta() and update_post_meta() accidentally, without custom post types field and it worked, but I don't know, if this work without issues from WordPress or Types Plugin in Future. I don't need, that Admin see this field. I change this field only with PHP. My Question: Is this okay, if I do add_post_meta($post_id, "wpcf-somepost","XYZ") or update_post_meta($post_id, "wpcf-somepost", "ABC") , if I not added previously "wpcf-somepost" custom Field with Types Plugin?
Minesh is on vacation, so I'll take over here if that's okay.
Regarding your second point, Types fields are stored in the wp_postmeta table with a key that is the field slug prefixed with "wpcf-", e.g. a field with a slug of "description" would be stored with a key of "wpcf-description".
They are just the same as other post meta, so there is no reason you cannot save and update them using standard WordPress functions. Just be aware that some of the more complex fields, e.g. checkboxes, dates, are stored in a particular format, but most fields are stored as text strings.
For the first point I will need to investigate further and see if I can find a solution. I'll get back to you later.
I'm just re-reading this and am I right in saying that you want to hide the Post Relationship meta box where child posts can be added at the bottom of a parent post edit screen?
Is it enough for you to simply go to the Screen Options tab at the top of the page and uncheck the Post Relationships option.
That will hide that box for all users for all posts of that type.
Or do you need a more secure option that cannot be undone?
I deactived it already from Screen Options, but it does work, how I want. Field is hidden, but get updated automatically, if admin updates some post.
I have an Idea. I added Field Group e.g. "FG" and in that group custom post field e.g. "CPF". I have a problem, that this field is updating automatically, because it is field of some post. Now I removed this "FG" and therefore Admin cannot update it. I can update it only with update_post_meta() . I need update_post_meta(), because I save page Stats with AJAX "POST" Method.
I want to ask you, if I don't get some Issue from wordpress or Toolset Plugins, if I do so, how I described above. If not, then my Problem is resolved.
The Types fields are just stored as standard post meta, and there should be no problem you updating them directly via code instead of in the WordPress admin pages.
The only issue I foresee is if you have de-activated the field group containing this field then, if you tried to make a new View or CRED form using this field, for example, it wouldn't be automatically recognised and available in the UI. But once you have your Views and forms etc. created the Field Group doesn't need to be active, and you can always temporarily re-activate it if you need to make some changes to your site.