You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发WordPress插件时无法显示公共自定义文章类型的问题

问题解决:获取所有公共自定义文章类型

你的代码仅返回post、page是因为$args里的_builtin => true参数,它会限制只获取WordPress内置的文章类型,而book、movie这类自定义文章类型的_builtin属性为false,所以被过滤掉了。

解决方案

根据需求调整_builtin参数即可:

1. 获取所有公共文章类型(含内置+自定义)

移除_builtin参数,就能返回所有public为true的文章类型:

private function test_get_post_types()
{
    $post_types_arr =  array();

    $args = array(
        'public'   => true
        // 移除 _builtin 参数,不再限制仅内置类型
    );

    $output = 'objects';
    $operator = 'and';

    $post_types = get_post_types($args, $output, $operator);

    foreach ($post_types as $post_type) {
        $post_types_arr[$post_type->name] = ucfirst($post_type->name);
    }

    return $post_types_arr;
}

2. 仅获取公共自定义文章类型(排除内置的post、page等)

如果只想展示自定义的公共文章类型,把_builtin设为false:

$args = array(
    'public'   => true,
    '_builtin' => false
);

补充说明

get_post_types函数的_builtin参数作用:

  • true:仅返回WordPress原生内置类型(post、page、attachment等)
  • false:仅返回自定义注册的类型
  • 不设置该参数:返回所有符合public条件的类型

内容的提问来源于stack exchange,提问作者user3722579

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 15:17:19