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

时间:2019-08-26 作者:Severin

我想将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”尚未注册。为什么会这样?

2 个回复
最合适的回答,由SO网友:Severin 整理而成

无法注册自定义字段(post\\u meta)。

register\\u meta仅与REST API配合使用。

SO网友:JChops

使用起来可能会容易得多https://wordpress.org/plugins/advanced-custom-fields/

如果没有签出this post 这就指向了this page

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在