get_post_types() WordPress Function

The get_post_types() function is used to retrieve a list of all the available post types in WordPress. This function can be used to check if a specific post type exists before using it in your code.

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

Get a list of all registered post type objects.


Description

Top ↑

See also


Top ↑

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

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


Top ↑

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
VersionDescription
2.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More