custom post types - how can I register a post_meta field in an existing CPT and then call it again with get_post_custom()?

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

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?

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?

Share Improve this question edited Aug 27, 2019 at 19:04 Severin asked Aug 26, 2019 at 22:07 SeverinSeverin 551 gold badge1 silver badge8 bronze badges 7
  • 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 Commented Aug 27, 2019 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 Commented Aug 27, 2019 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 Commented Aug 28, 2019 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 Commented Aug 28, 2019 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 Commented Sep 5, 2019 at 23:03
 |  Show 2 more comments

2 Answers 2

Reset to default 0

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

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

custom fields (post_meta) cannot be registered.

register_meta only works in conjunction with the REST API.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745181140a4615423.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信