get_post_types( array|string $args = array(), string $output = 'names', string $operator = 'and' )

Get a list of all registered post type objects.


Description Description

See also See also


Top ↑

Parameters Parameters

$args

(array|string) (Optional) An array of key => value arguments to match against the post type objects.

Default value: array()

$output

(string) (Optional) The type of output to return. Accepts post type 'names' or 'objects'.

Default value: 'names'

$operator

(string) (Optional) 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 value: 'and'


Top ↑

Return Return

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


Top ↑

Source Source

File: wp-includes/post.php

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 );
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.9.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by tleifj

    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 2 content
    Contributed by Codex

    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 3 content
    Contributed by Codex

    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 4 content
    Contributed by Md. Jwel Miah

    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 7 content
    Contributed by Mostafa Soufi

    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 8 content
    Contributed by kantbtrue

    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.