get_post_types

Home » Functions » get_post_types

Function Name: get_post_types

The get_post_types() function in WordPress is used to retrieve all registered post types. A post type is a specific type of content that can be created and managed in WordPress, such as posts, pages, or custom post types. This function returns an array of post types, including the built-in post types and any custom post types that have been registered.

This function can be useful for plugin and theme developers who want to interact with specific post types. For example, a plugin that adds custom fields to a post type can use this function to ensure that the custom fields are only added to the appropriate post types.

Syntax:

get_post_types( $args, $output, $operator )

Parameters:

  • $args: (array) Optional. An array of arguments to filter the results by.
  • $output: (string) Optional. The type of output to return. Can be ‘names’ (default), ‘objects’, or ‘ui’.
  • $operator: (string) Optional. How to compare the values of each argument. Can be ‘and’ (default) or ‘or’.

Example Usage:

$post_types = get_post_types( array( 'public' => true ) );

foreach ( $post_types as $post_type ) {
    echo '<p>' . $post_type . '</p>';
}

In the example above, we are using the get_post_types() function to retrieve all post types that are set to ‘public’. We then loop through the array of post types and print out each post type name in a paragraph tag.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.