函数文档

get_post_types()

💡 云策文档标注

概述

get_post_types() 函数用于获取所有已注册的文章类型对象列表。它支持通过参数筛选输出,并可返回名称数组或对象数组,常用于自定义查询和界面开发。

关键要点

  • 函数返回所有已注册文章类型的名称或对象数组,默认输出为名称数组
  • 支持三个参数:$args(筛选条件数组)、$output(输出类型,'names' 或 'objects')、$operator(逻辑操作符,'and'、'or'、'not')
  • 内部使用 wp_filter_object_list() 实现筛选,基于全局变量 $wp_post_types
  • 常用于权限检查、模板生成、菜单构建等场景,被多个核心函数和类调用

代码示例

// 获取所有公开的自定义文章类型名称
$args = array(
    'public' => true,
    '_builtin' => false
);
$post_types = get_post_types( $args, 'names', 'and' );

// 获取特定文章类型对象并访问属性
$args = array('name' => 'movies');
$post_types = get_post_types( $args, 'objects' );
foreach ( $post_types as $post_type ) {
    echo 'Name: ' . $post_type->name;
    echo 'Singular Name: ' . $post_type->labels->singular_name;
}

注意事项

  • $args 参数可接受多种键值对,如 public、show_ui、show_in_rest 等,用于精确筛选
  • 设置 '_builtin' => false 可排除 WordPress 内置文章类型(如 post、page),仅返回自定义类型
  • 输出类型为 'objects' 时,可直接访问 WP_Post_Type 对象的属性和方法,便于高级操作

📄 原文内容

Gets a list of all registered post type objects.

Description

See also

Parameters

$argsarray|stringoptional
An array of key => value arguments to match against the post type objects.

Default:array()

$outputstringoptional
The type of output to return. Either 'names' or 'objects'. Default 'names'.
$operatorstringoptional
The logical operation to perform. 'or' means only one element from the array needs to match; 'and' means all elements must match; 'not' means no elements may match. Default 'and'.

Return

string[]|WP_Post_Type[] An array of post type names or objects.

Source

function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
	global $wp_post_types;

	$field = ( 'names' === $output ) ? 'name' : false;

	return wp_filter_object_list( $wp_post_types, $args, $operator, $field );
}

Changelog

Version Description
2.9.0 Introduced.

User Contributed Notes

  1. Skip to note 10 content

    Argument values for $args include:

    • public – Boolean. If true, only public post types will be returned.
    • publicly_queryable – Boolean
    • exclude_from_search – Boolean
    • show_ui – Boolean
    • capability_type
    • hierarchical
    • menu_position
    • menu_icon
    • permalink_epmask
    • rewrite
    • query_var
    • show_in_rest – Boolean. If true, will return post types whitelisted for the REST API
    • _builtin – Boolean. If true, will return WordPress default post types. Use false to return only custom post types.

  2. Skip to note 11 content

    Output a list of only custom post types which are public
    By setting '_builtin' to false, we exclude the WordPress built-in public post types (post, page, attachment, revision, and nav_menu_item) and retrieve only registered custom public post types.

     true,
       '_builtin' => false
    );
     
    $output = 'names'; // 'names' or 'objects' (default: 'names')
    $operator = 'and'; // 'and' or 'or' (default: 'and')
     
    $post_types = get_post_types( $args, $output, $operator );
     
    if ( $post_types ) { // If there are any custom public post types.
     
        echo '<ul>';
     
        foreach ( $post_types  as $post_type ) {
            echo '<li>' . $post_type . '</li>';
        }
     
        echo '</ul>';
     
    }
    ?>

  3. Skip to note 12 content

    Retrieve a named post type as an object
    This example uses the 'object' output to retrieve the post type called ‘movies’ and display its name, singular name and menu icon (an URL):

     'movies',
    );
    
    $post_types = get_post_types( $args, 'objects' );
    
    foreach ( $post_types  as $post_type ) {
       echo '<p>Custom Post Type name: ' . $post_type->name . "<br />n";
       echo 'Single name: ' . $post_type->labels->singular_name . "<br />n";
       echo 'Menu icon URL: ' . $post_type->menu_icon . "</p>n";;
    }
    ?>

  4. Skip to note 13 content

    Get post types array with name => singular name

    function prefix_get_post_types() {
        $post_types = get_post_types([], 'objects');
        $posts = array();
        foreach ($post_types as $post_type) {
            $posts[$post_type->name] = $post_type->labels->singular_name;
        }
        return $posts;
    }

    You can use this function in a select dropdown option where user can select a post type form existing post types.

  5. Skip to note 18 content

    Remove specific post type

    $post_types = get_post_types( array( 'public' => true, '_builtin' => true ), 'names', 'and' );
    // remove attachment from the list
    unset( $post_types['attachment'] );
    echo '<ul>';
    foreach ( $post_types  as $post_type ) {
        echo '<li>' . $post_type . '</li>';
    }
    echo '</ul>';