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 | Show 2 more comments2 Answers
Reset to default 0It'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
get_post_custom()
does not include the metatest_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 byregister_meta()
. – Sally CJ Commented Aug 27, 2019 at 4:29if(!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