-
-
Save dgoze/5db72d5f5eab17bd2410a93a26516a59 to your computer and use it in GitHub Desktop.
Check if a custom post type supports archives
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
<?php | |
/** | |
* Check if post type supports an archive | |
* | |
* @param string $post_type post type name | |
* @uses get_post_type | |
* @global object $post | |
* @returns boolean | |
* @author Joshua David Nelson | |
*/ | |
if( !function_exists( 'cpt_has_archive' ) ) { | |
function cpt_has_archive( $post_type ) { | |
if( !is_string( $post_type ) || !isset( $post_type ) ) | |
return false; | |
// find custom post types with archvies | |
$args = array( | |
'has_archive' => true, | |
'_builtin' => false, | |
); | |
$output = 'names'; | |
$archived_custom_post_types = get_post_types( $args, $output ); | |
// if there are no custom post types, then the current post can't be one | |
if( empty( $archived_custom_post_types ) ) | |
return false; | |
// check if post type is a supports archives | |
if ( in_array( $post_type, $archived_custom_post_types ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
// if all else fails, return false | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment