首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在现有的CPT中注册post_meta字段,然后使用get_post_custom()再次调用它?

如何在现有的CPT中注册post_meta字段,然后使用get_post_custom()再次调用它?
EN

WordPress Development用户
提问于 2019-08-26 22:07:10
回答 2查看 304关注 0票数 1

我想添加一个post_meta (test_meta_1234)字段到一个现有的CPT (组织者)的外部插件。

对于register_meta(),它不起作用。但是,我可以使用register_taxonomy()在同一个CPT中添加一个分类法。

代码示例:

代码语言:javascript
复制
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_meta (test_meta_1234)。

为什么我不能用post_meta ()来查看get_post_custom字段?

更新1:

CPT不支持“自定义字段”,所以现在首先检查并添加以下内容:

代码语言:javascript
复制
`if(!post_type_supports( 'organizer', 'custom-fields' )){
 add_post_type_support( 'organizer', 'custom-fields' );
}`

自定义字段“test_meta_1234”尚未注册。为什么要这样?

EN

回答 2

WordPress Development用户

回答已采纳

发布于 2019-09-05 23:06:12

无法注册自定义字段(post_meta)。

register_meta只与REST一起工作。

票数 0
EN

WordPress Development用户

发布于 2019-08-27 19:21:43

仅仅使用https://wordpress.org/plugins/advanced-custom-fields/可能要容易得多

如果没有,请查看这个职位,它将指向此页

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/345961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档