get_post_types( array|string $args = array(), string $output = ‘names’, string $operator = ‘and’ ): string[]|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'.

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'.

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

VersionDescription
2.9.0Introduced.

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.

    <?php
    $args = array(
       'public'   => 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):

    <?php
    $args = array(
     	'name' => '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 14 content

    Display the HTML dropdown list of Post Types.

    <?php
    // Get post types
    $args       = array(
    	'public' => true,
    );
    $post_types = get_post_types( $args, 'objects' );
    ?>
    
    <select class="widefat" name="post_type">
        <?php foreach ( $post_types as $post_type_obj ):
            $labels = get_post_type_labels( $post_type_obj );
            ?>
            <option value="<?php echo esc_attr( $post_type_obj->name ); ?>"><?php echo esc_html( $labels->name ); ?></option>
        <?php endforeach; ?>
    </select>
  6. Skip to note 16 content

    Display the HTML dropdown list of Post Types, including custom post types

    <?php
    $post_types = get_post_types(array('public' => true), 'names', 'and');
    ?>
    <select class="" name="post_type">
      <?php
    
      foreach ($post_types  as $post_type) {
      ?>
        <option value="<?php echo esc_attr($post_type); ?>"><?php echo esc_html($post_type); ?></option>
      <?php
      }
      ?>
    </select>
    ?>
  7. Skip to note 17 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>';

You must log in before being able to contribute a note or feedback.