Skip to content

Instantly share code, notes, and snippets.

@baras
Created January 16, 2019 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baras/46e151a91d321a214b70feec7222cd37 to your computer and use it in GitHub Desktop.
Save baras/46e151a91d321a214b70feec7222cd37 to your computer and use it in GitHub Desktop.
Get custom post types and page templates in php
function get_custom_post_types_and_page_templates() {
global $wp_post_types;
// Public custom post types.
$args = [
'public' => true,
'_builtin' => false,
];
// Get the lists and add a suffix which denotes the type, Page or Post.
$post_types = preg_filter( '/$/', ' (Post)', get_post_types( $args ) );;
$templates = preg_filter( '/$/', ' (Page)', wp_get_theme()->get_page_templates() );
// Combine the arrays and sort alphabetically (maintain index association).
$output = array_merge( $post_types, $templates );
asort( $output );
echo json_encode( $wp_post_types );
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment