how can I register a post_meta field in an existing CPT and then call it again with get_post_custom()?
-
16-04-2021 - |
質問
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?
解決 2
custom fields (post_meta) cannot be registered.
register_meta only works in conjunction with the REST API.
他のヒント
It'll probably be way easier to just use https://wordpress.org/plugins/advanced-custom-fields/
所属していません wordpress.stackexchange