I recently ran into a situation while working on a WordPress site where I needed to query for custom post types that were created by a plugin I was using, but I didn’t know the name of the specific post type. I consulted the plugin developer’s documentation, and did a bit of ‘Googling,’ but came up empty.
Before getting too far into my search, I decided to go about it a different way. Rather than looking for a specific post type without knowing what that post type is called, I looked for a way to output all available post types in WordPress.
As luck would have it, WordPress has a get_post_types() function that returns the names of all standard post types and custom post types. So, to find out all the post types available to us, we’ll use the get_post_types() function along with the print_r() function in PHP to return and display a result. You can drop this code into any page or template file and view the result. I’ve added <pre></pre> tags to give us a cleaner, more readable output.
<?php $show_post_types = get_post_types(); ?> <pre> <?php print_r($show_post_types); ?> </pre>
You should see results returned that include all the built in WordPress Post Types, along with any other available post types that may have been added by any plugins or your theme. In this example, we have several WordPress plugins installed, including bbpress, Advanced Custom Fields, and The Events Calendar that have created their own post types.
Array ( [post] => post [page] => page [attachment] => attachment [revision] => revision [nav_menu_item] => nav_menu_item [forum] => forum [topic] => topic [reply] => reply [acf-field-group] => acf-field-group [acf-field] => acf-field [tribe_venue] => tribe_venue [tribe_organizer] => tribe_organizer [tribe_events] => tribe_events )
Since we aren’t concerned with the built WordPress post types, we can tweak our code a bit so that we don’t return those post types. The code below adds arguments to our initial query so that we are only returning public post types (visible to authors and readers and are ‘queryable’) and not returning the built in post types like posts and pages.
<?php $args = array( 'public' => true, '_builtin' => false );?> <?php $show_post_types = get_post_types( $args ); ?> <pre> <?php print_r($show_post_types); ?> </pre>
Now we should be looking at only public post types that aren’t built in to WordPress.
Array ( [forum] => forum [topic] => topic [reply] => reply [tribe_events] => tribe_events )
This ‘should’ make it easy to determine just what the name of the post type is that we want to use in our query. Since I was looking for an events plugin post type, it’s a pretty safe bet that tribe_events is the post type I am looking for. (and it was!)