WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get post types

Description

Returns the registered post types as found in $wp_post_types.

Usage

 <?php get_post_types$args$output$operator ?> 

Parameters

$args
(array) (optional) An array of key value arguments to match against the post types.
Default: array()

Some of these 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
  • _builtin - Boolean. If true, will return WordPress default post types. Use false to return only custom post types.
$output
(string) (optional) The type of output to return, either 'names' or 'objects'.
Default: 'names'
$operator
(string) (optional) Operator (and/or) to use with multiple $args.
Default: 'and'

Return Values

array 
A list of post names or objects.

Examples

Default Usage

The call to get post types returns the registered post types.

<?php $post_types = get_post_types(); ?>

Output a list all registered post types

<?php
foreach ( get_post_types( '', 'names' ) as $post_type ) {
   echo '<p>' . $post_type . '</p>';
}
?>

Output a list of all public custom post types

The built-in public post types are post, page, and attachment. By setting '_builtin' to false, we will exclude them and show only the custom public post types.

<?php

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

$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'

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

foreach ( $post_types  as $post_type ) {

   echo '<p>' . $post_type . '</p>';
}

?>

Output a named post type

This example uses the 'object' output to retrieve the post type called 'property':

<?php

$args = array(
   'name' => 'property'
);

$output = 'objects'; // names or objects

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

foreach ( $post_types  as $post_type ) {

   echo '<p>' . $post_type->name . '</p>';
}

?>

Notes

Be careful when retrieving "public" custom post-types that were registered using the register_post_type() function: the inputs to the register_post_type function are not intelligently processed, so if you verbosely set options for publicly_queryable, show_ui, show_in_nav_menus, and exclude_from_search, this is NOT considered equivalent to setting the public option and querying for public post-types will not yield results that were defined with the equivalent explicit arguments. See bug 18950.

WordPress builtin post types:

  • post - WordPress built-in post type
  • page - WordPress built-in post type
  • attachment - WordPress built-in post type
  • revision - WordPress built-in post type
  • nav_menu_item - WordPress built-in post type (Since 3.0)
  • custom_css - Customizer specific
  • customize_changeset - Customizer specific
  • custom post type - any custom post type (Since 3.0)

Change Log

  • 3.0 :
    • Added the operator parameter.
  • Since: 2.9

Source File

get_post_types() is located in wp-includes/post.php.

Related

Post Types: register_post_type(), add_post_type_support(), remove_post_type_support(), post_type_supports(), post_type_exists(), set_post_type(), get_post_type(), get_post_types(), get_post_type_object(), get_post_type_capabilities(), get_post_type_labels(), is_post_type_hierarchical(), is_post_type_archive(), post_type_archive_title()

See also index of Function Reference and index of Template Tags.