Created
January 16, 2019 08:44
-
-
Save baras/46e151a91d321a214b70feec7222cd37 to your computer and use it in GitHub Desktop.
Get custom post types and page templates in php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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