WordPress.org

Ready to get started?Download WordPress

Codex

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

Difference between revisions of "Function Reference/get post types"

(Alerting users to confusing usage of the "public" option)
(Remove parameters you'd never or can't use.)
Line 10: Line 10:
   
 
{{Parameter|$args|array|An array of key value arguments to match against the post types.|optional}}
 
{{Parameter|$args|array|An array of key value arguments to match against the post types.|optional}}
* label
+
Some of these include:
* singular_label
 
* description
 
 
* public - ''Boolean. If true, only public post types will be returned.''
 
* public - ''Boolean. If true, only public post types will be returned.''
 
* publicly_queryable - ''Boolean''
 
* publicly_queryable - ''Boolean''
Line 16: Line 16:
 
* show_ui - ''Boolean''
 
* show_ui - ''Boolean''
 
* capability_type
 
* capability_type
* edit_cap
 
* edit_type_cap
 
* edit_others_cap
 
* publish_others_cap
 
* read_cap
 
* delete_cap
 
 
* hierarchical
 
* hierarchical
* supports
 
* register_meta_box_cb
 
* taxonomies
 
 
* menu_position
 
* menu_position
 
* menu_icon
 
* menu_icon
Line 31: Line 22:
 
* rewrite
 
* rewrite
 
* query_var
 
* query_var
* _builtin - ''Boolean. If true, will return WordPress default post types. Use false to return only custom post types.''
+
* _builtin - ''Boolean. If true, will return WordPress default post types. Use false to return only custom post types.''
* _edit_link
 
   
 
{{Parameter|$output|string|The type of output to return, either 'names' or 'objects'.|optional|'names'}}
 
{{Parameter|$output|string|The type of output to return, either 'names' or 'objects'.|optional|'names'}}

Revision as of 04:28, 5 November 2011

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: None

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 mulitple $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 
$post_types=get_post_types('','names'); 
foreach ($post_types as $post_type ) {
  echo '<p>'. $post_type. '</p>';
}
?>

Output a list of all public custom 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_queriable, 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
  • mediapage - 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 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.