Skip Navigation

[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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Author
Posts
#595356

Hi,

I found this Code, how to update_post_meta for custom post types: https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

function is_project_overdue( $post_id, $post ){
    if ( 'project' == $post->post_type ) {
        $deadline = get_post_meta( $post_id, 'wpcf-deadline', true );
        $now = time();
        if ( !empty($deadline) && $now > $deadline ) {
            update_post_meta( $post_id, 'wpcf-overdue', 1 );
        }
    }
}
add_action( 'save_post', 'is_project_overdue', 30, 2 );

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?

Thanks

#595479

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

More info:
https://codex.wordpress.org/Function_Reference/remove_meta_box

If you need custom programming work for your site, please feel free to contact our certified partners:
=> https://toolset.com/consultant/

#596448

Hi Minesh,

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?

best regards

#596844

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Christian

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.

#597172

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Christian

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?

#597249

Hi Nigel,

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.

best regards

#597604

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Christian

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.

#599170

Hi Nigel,

Thanks for your answer. I don't use this field in Views or Creds, therefore is my problem now resolved.

best regards

#599171

Resolved