Using WordPress ‘is_post_type_hierarchical()’ PHP function

The is_post_type_hierarchical() WordPress PHP function determines if a post type is hierarchical.

Usage

is_post_type_hierarchical($post_type);

Example:

Input:

is_post_type_hierarchical('page');

Output:

true

Parameters

  • $post_type (string): Required. The name of the post type.

More information

See WordPress Developer Resources: is_post_type_hierarchical()

Examples

Check if a custom post type is hierarchical

This example checks if a custom post type, ‘products’, is hierarchical.

if (is_post_type_hierarchical('products')) {
    echo 'Products post type is hierarchical.';
} else {
    echo 'Products post type is not hierarchical.';
}

Display hierarchical post types

This example displays the names of all registered hierarchical post types.

$post_types = get_post_types();
foreach ($post_types as $post_type) {
    if (is_post_type_hierarchical($post_type)) {
        echo $post_type . ' is hierarchical.<br>';
    }
}

Check if the current post’s post type is hierarchical

This example checks if the post type of the current post in the loop is hierarchical.

global $post;
if (is_post_type_hierarchical($post->post_type)) {
    echo 'The current post type is hierarchical.';
} else {
    echo 'The current post type is not hierarchical.';
}

Display only non-hierarchical post types

This example displays the names of all registered non-hierarchical post types.

$post_types = get_post_types();
foreach ($post_types as $post_type) {
    if (!is_post_type_hierarchical($post_type)) {
        echo $post_type . ' is not hierarchical.<br>';
    }
}

Check if post type exists and is hierarchical

This example checks if a post type exists and if it’s hierarchical.

$post_type_name = 'projects';
if (post_type_exists($post_type_name) && is_post_type_hierarchical($post_type_name)) {
    echo 'The ' . $post_type_name . ' post type exists and is hierarchical.';
} else {
    echo 'The ' . $post_type_name . ' post type either does not exist or is not hierarchical.';
}