我想将post\\u meta(test\\u meta\\u 1234)字段添加到外部插件的现有CPT(组织者)中。
对于register\\u meta(),它不起作用。但我可以使用register\\u taxonomy()将一个分类法添加到相同的CPT中。
代码示例:
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);
分类法被添加到CPT中,但没有添加到post\\u meta(test\\u meta\\u 1234)。
为什么我无法使用get\\u post\\u custom()查看post\\u meta字段?
更新1:
CPT不支持“自定义字段”,因此现在首先检查并添加以下内容:
`if(!post_type_supports( \'organizer\', \'custom-fields\' )){
add_post_type_support( \'organizer\', \'custom-fields\' );
}`
自定义字段“test\\u meta\\u 1234”尚未注册。为什么会这样?