carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

reference post's existing custom fields

Open ghost opened this issue 8 years ago • 3 comments

I was hoping to simply replace WP's custom fields with CF. I have existing post meta data on an old site that I am currently accessing via get_post_meta(). I was hoping that I could simply create Carbon fields via Field::make(‘text’, ‘client’) and it would reference this existing post data.

Here is how I am currently accessing the existing metadata

get_post_meta($post->ID, 'client', true);

Here is my code:

		Container::make('post_meta', 'Project Info')
		->where('post_type', '=', 'project')
		->add_fields([
			Field::make('text', 'client'),
		]);

I see that I can access the carbon meta with

get_post_meta($post->ID, '_client', true);

But I need to access the existing ‘client’ meta not ‘_client’. Is this possible?

ghost avatar Nov 17 '18 09:11 ghost

If you're comfortable working with SQL you can always migrate your old data; for example:

UPDATE `wp_postmeta` SET `meta_key`='_crb_client' WHERE `meta_key` = 'client';

xsynaptic avatar Nov 23 '18 10:11 xsynaptic

I'm sure there a logical reason for this but it would really nice if there was an option to use existing field names without having to recreate them. A flag or filter that would not add the underscore to the meta name.

ghost avatar Dec 01 '18 22:12 ghost

Chiming in here with a partial answer. The underscore in front of the meta key name has the effect of hiding both the key and the value from the default (classic editor) Custom Fields meta box.

Image of Classic Editor

Moving forward into the 5.0 era of WP, this won't be much of a factor. And if you are 100% committed to Gutenberg in your theme, I'd venture that you can safely run the above SQL query to get rid of the underscore completely.

SteveRyan-ASU avatar Dec 19 '18 16:12 SteveRyan-ASU