Special Post Type Functions WordPress


WordPress features many different post type
specific functions to make working with custom post
types that much easier. In this section, you will review some of the more common functions you
might use when building your websites.
To return a list of all registered post types in WordPress, you’ll use the get_post_types()function.
<?php get_post_types( $args, $output, $operator ); ?>
This function accepts three optional parameters:
1.  $args— An array of arguments to match against the post type.
2.  $output— The type of output to return, either namesor objects. Defaults to names.
3.  $operator— Operator to use with multiple $args. Defaults to and.
Using the get_post_types()function, use the following to return a list of all custom post types
registered in WordPress:
$args = array(
'public' => true,
'_builtin' => false
);
$post_types = get_post_types( $args, 'names', 'and' );
foreach ( $post_types as $post_type ) {
echo '<p>'. $post_type. '</p>';
}
As shown in the preceding code, you’ll set two arguments in the $argsarray: publicand _
builtin. The publicargument will only return custom post types that are set to be publicly
viewable. The _builtinargument is set to false, which will not return default post types like posts
and pages. You also set the $outputargument to return just the post type name, and the $operator
argument to use “and” for the multiple $argsyou passed to the function.
To determine what post type a piece of content is, you’ll use the get_post_type()function: