无法为帖子设置自定义元字段

时间:2019-08-18 作者:Finglish

我正在尝试向自定义帖子类型添加一些自定义元变量,但在转储帖子元数据时,我看不到新的变量,并且无法在中访问它

这是我的函数中的帖子定义。php文件

function register_team_post(){
    register_post_type(\'team\', [
        \'public\'=>true,
        \'labels\'=>array(
            \'name\'=>\'Team\',
            \'add_new_item\'=>\'Add New Team Member\',
            \'edit_item\'=>\'Edit Team Member\',
            \'all_items\'=>\'All Team Member\'
        ),
        \'menu_icon\'=>\'dashicons-businessman\',
        \'show_in_rest\' => true,
        // \'supports\' => array(\'editor\',\'title\', \'custom-fields\'),
        \'supports\' => array(\'editor\',\'title\'),
        \'rewrite\' => array(\'slug\' => \'teams\'),
        \'template\' => array(
            array( \'dqcblocks/member\' )
        )
    ]);

    $fields = [
        \'team_member_portrait\',
        \'team_member_name\',
        \'team_member_title\',
        \'team_member_description\'
    ];

    $args = array(\'show_in_rest\' => true, \'type\' => \'string\');

    foreach($fields as $field){
        register_post_meta( \'team\', $field, $args);
    }
}

add_action(\'init\', \'register_team_post\');
如果我创建了一个团队帖子并尝试转储元字段,但我的新字段没有出现:

$meta_values = get_post_meta( get_the_ID() );
var_dump( $meta_values );
结果:

array(2) {
  ["_edit_lock"]=>
  array(1) {
    [0]=>
    string(12) "1566159392:1"
  }
  ["_edit_last"]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
}
我还尝试在古腾堡块中使用我的自定义元字段。

attributes: {
    portrait: {
      type: "string",
      source: "meta",
      meta: "team_member_portrait",
    },
    name: {
        type: "string",
        source: "meta",
        meta: "team_member_name",
    },
    title: {
      type: "string",
      source : "meta",
      meta: "team_member_title",
    },
    description: {
      type: "string",
      source: "meta",
      meta: "team_member_description",
    }
  }
但当我添加块并保存变量时,变量不会更新并保持为空。

我是否正确使用register\\u post\\u meta?

3 个回复
SO网友:S.Walsh

注册自定义帖子类型时,请确保custom-fields 中存在supports => array(\'editor\',\'title\', \'custom-fields\'). 这是元字段的遗留措辞,在CPT中声明支持时通常会被错误地忽略。

在进行上述更改之后,您的团队CPT应该使用元值。最初在古腾堡,您仍然可能会遇到元值“未定义”,因为它们没有默认值集。可以在注册Post Meta eg时设置默认值。\'default\' => \'test string\' 或在块属性中。

注意。此问题发布后,文档已更新ref: trac #47866参考最新版本WordPress REST API 查看所有更改

SO网友:Sunny Johal

对于阅读此旧线程的任何其他人:

这是应该怎么做的


function register_team_post() {
    register_post_type(
        \'team\',
        [
            \'public\'       => true,
            \'labels\'       => [
                \'name\'         => \'Team\',
                \'add_new_item\' => \'Add New Team Member\',
                \'edit_item\'    => \'Edit Team Member\',
                \'all_items\'    => \'All Team Member\',
            ],
            \'menu_icon\'    => \'dashicons-businessman\',
            \'show_in_rest\' => true,
            \'supports\'     => [ \'editor\', \'title\', \'custom-fields\' ],
            \'rewrite\'      => [ \'slug\' => \'teams\' ],
            \'template\'     => [ [ \'dqcblocks/member\' ] ],
        ]
    );

    $fields = [
        \'team_member_portrait\',
        \'team_member_name\',
        \'team_member_title\',
        \'team_member_description\',
    ];

    $args = [
        \'object_subtype\' => \'team\', // this is the correct place to pass the post type.
        \'show_in_rest\'   => true,
        \'type\'           => \'string\',
    ];

    foreach ( $fields as $field ) {
        register_meta( \'post\', $field, $args );
    }
}

add_action( \'init\', \'register_team_post\' );

SO网友:Den Pat

尝试在init上启动它,而不是在register\\u post\\u类型中,如下所示:

add_action(\'init\',\'345376_register_metaof_team\');
function 345376_register_metaof_team(){

$object_type = \'post\';
$args = array(
    \'type\'         => \'string\',
    \'description\'  => \'A meta key associated with post views.\',
    \'single\'       => true,
    \'show_in_rest\' => true,
);

register_meta( $object_type, \'post_views\', $args );

}

相关推荐

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

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