1

I would like to add a post_meta (test_meta_1234) field to an existing CPT (organizer) of an external plugin.

With register_meta() it doesn't work. But a taxonomy I can add to the same CPT with register_taxonomy().

Code Sample:

register_meta('post', 'test_meta_1234', array(
    'object_subtype' => 'organizer',
    'show_in_rest' => true,
    'single' => true,
    'type' => 'string',
    'description' => 'Test Meta 1234',
  )
);

register_taxonomy(
'genre',
'organizer',
 array(
    'label' => __( 'Genre' ),
    'rewrite' => array( 'slug' => 'genre' ),
    'hierarchical' => true,
 )
);

$otherPostTypesFull = new stdClass();
$otherPostTypes     = get_post_types();
foreach($otherPostTypes as $postType => $postTypeSlug){
 $args = array(
              'post_type'         => $postTypeSlug,
              'numberposts'             => -1
            );
 foreach(get_posts( $args ) as $faPosts){
  $otherPostTypesFull->$postTypeSlug->post_meta   = get_post_custom($faPosts->ID);
  $otherPostTypesFull->$postTypeSlug->taxonomies  = get_object_taxonomies( $postTypeSlug, 'objects' );
 }
}
var_dump($otherPostTypesFull);

The taxonomy is added to the CPT, but not the post_meta (test_meta_1234).

Why can't I see the post_meta field with get_post_custom()?


update 1:

The CPT did not support 'custom-fields', so now first check and add this:

`if(!post_type_supports( 'organizer', 'custom-fields' )){
 add_post_type_support( 'organizer', 'custom-fields' );
}`

The custom-field 'test_meta_1234' is still not registered yet. Why that?

  • If get_post_custom() does not include the meta test_meta_1234, then it's likely that your post doesn't have that meta. And does the post type actually support custom fields (custom-fields)? It's required by register_meta(). – Sally CJ Aug 27 at 4:29
  • Many thanks to Sally for the valuable input. The CPT 'organizer' support custom fields do not. I have added if(!post_type_supports( 'ecwd_organizer', 'custom-fields' )){ add_post_type_support( 'ecwd_organizer', 'custom-fields' ); } and now supports the CPT 'organizer', 'custom-fields'. But the custom field 'test_meta_1234' is still not registered. Why that? – Severin Aug 27 at 18:55
  • Actually, what do you mean by "registered"? Are you sure the post has the specified custom field? How did you add the custom field to the post? And how do you register the CPT - what's your code or the plugin if you're using a plugin? – Sally CJ Aug 28 at 3:34
  • I thought that with register_meta() a custom-field (post-meta) can be registered identical to register_taxonomy() a caregory/keyword. Is this the case? – Severin Aug 28 at 20:06
  • 1
    Thank you so much, Sally. I have now understood that custom fields cannot be registered and is register_meta for the REST API. Many thanks for the inputs. – Severin Sep 5 at 23:03
0

It'll probably be way easier to just use https://wordpress.org/plugins/advanced-custom-fields/

If not check out this post which points you to this page

  • Many thanks for the input. The Custom Post Type already exists from another plugin. The post_meta field should be stored in the system so that I can reuse it in a table as a column. So I don't need a MetaBox or any other plugin, because they manage the data for the field post_meta internally in the plugin. I want to store the additional field with hardCode. – Severin Aug 27 at 20:24
0

custom fields (post_meta) cannot be registered.

register_meta only works in conjunction with the REST API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.