Skip Navigation

[Resolved] Displaying a custom field on a specific page

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 1 voice.

Last updated by Saul Baizman 3 weeks, 6 days ago.

Assisted by: Minesh.

Author
Posts
#2808661

Hi there,

Long-time Toolset user here. I know Toolset allows users to display a custom field group for a given post type (or other criteria) in the editor, but I'm trying to display a custom field on just one page. Is that possible?

I found a reference to a similar issue on https://toolset.com/forums/topic/displaying-a-custom-field-group-for-a-specific-page/, and it makes reference to this URL with some sample PHP code:

https://toolset.com/forums/topic/post-field-group-not-saving-correctly/page/2/#post-1199207

That URL, unfortunately, displays a 404 error message. It seems Toolset has done some housecleaning and removed older posts? If so, for those of us who use the "legacy" functionality, retaining those old posts would have been, and would still be, very helpful.

Thanks.

Saul

#2808758

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

if you want to show/hide the custom field based on the specific post/page than you need to use WordPress filter hook "remove_meta_box":

- Add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

	
function func_remove_specific_custom_fields() {
    global $post_type;
    global $post;
     global $current_user;
  
if($post_type=='your-post-type' and $post->ID==999999){
  
            remove_meta_box( 'wpcf-group-{YOUR-POST-FIELDGROUP-SLUG}', $post_type,'normal');
}
}
add_action('admin_head', 'func_remove_specific_custom_fields' );

Where:
- Replace '{YOUR-POST-FIELDGROUP-SLUG}' with your original custom fields group slug.
- Replace 'your-post-type' with your post type slug
- Replace 999999 with post/page ID

Please feel free to adjust the above code if required and I hope the above solution will help you to resolve your issue.

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

#2808840

Hi Minesh,

Thanks for your response. It mostly makes sense, except for one thing: the code is doing the opposite of what I want it to do. It's *hiding* a custom field group for a given post type and page ID combination. I want to *display* a custom field group for a given post type and page ID combination. Should I just negate the conditionals?

Here's what I mean:

if($post_type != 'your-post-type' and $post->ID != 999999){ ... }

Thanks.

Saul

#2808891

Minesh
Supporter

Languages: English (English )

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

In that case you should try to use the followng conditional statement:

Post type will be same for all posts - right? if yes:

In that case, you should try to use the following if condition.

if($post_type == 'your-post-type' and $post->ID != 999999){ ... }

#2808949

Ah, excellent. Thanks, Minesh! I appreciate it!

Saul

#2808962

Minesh,

I tried applying this code (but modified for my needs) on my site, and it didn't work. The edit screen I'm working with is a GravityView and not the typical page or post editor (neither the block editor nor the classic editor).

After adding some debugging statements, I can see that the remove_meta_box() function is running when it's supposed to. I have verified that all of the parameters to remove_meta_box() are correct, too. I tried changing the priority of add_action(), but this did nothing. I even moved the code out of my plugin and moved it into functions.php of my theme. Nothing.

What's the next step to troubleshoot this?

Saul

#2808964

Nevermind! I had to change the context from "normal" to "advanced." Maybe that will help someone else!

Saul