ワードプレスのis_post_type_hierarchical関数の使用方法・解説

概要

is_post_type_hierarchical関数は、特定の投稿タイプが階層的であるかどうかを調べるために用いることができます。主に、カスタム投稿タイプを利用する際や、階層的なコンテンツを扱うアプリケーションを開発する際に役立ちます。この関数は以下のような状況でよく使用されます。

  1. カスタム投稿タイプに対する管理インターフェースのカスタマイズ
  2. 階層的に表示されるコンテンツのオーバーライドリスト作成
  3. 階層的なナビゲーションメニューの生成
  4. カスタムクエリでの階層化した投稿の取得
  5. 投稿タイプを登録する時の条件分岐
  6. 階層的な投稿に関連するナビゲーションを強化
  7. 投稿タイプに基づく権限設定
  8. ブロックエディタでのカスタム投稿の表示方法に影響を与える

構文

is_post_type_hierarchical( string $post_type )

パラメータ

  • post_type (string): 調べたい投稿タイプのスラッグ。

戻り値

  • (bool): 指定した投稿タイプが階層的であればtrue、そうでなければfalse

関連する関数

使用可能なバージョン

  • WordPress 2.9.0以降

ワードプレスコアファイルのパス

  • wp-includes/post.php

サンプルコード

サンプル1: 階層的かどうかを確認

このコードは、特定のカスタム投稿タイプが階層的かどうかを確認し、結果を出力します。

$post_type = 'custom_type';
if (is_post_type_hierarchical($post_type)) {
    echo "{$post_type} は階層的です。";
} else {
    echo "{$post_type} は階層的ではありません。";
}

引用元: https://developer.wordpress.org/reference/functions/is_post_type_hierarchical/

サンプル2: 投稿タイプの登録時の条件分岐

このコードは、カスタム投稿タイプを登録する際に、その投稿タイプが階層的であるかを確認して、適切なオプションを設定します。

function create_custom_post_type() {
    $args = array(
        'hierarchical' => true  // 階層的投稿タイプに設定
    );

    if (!is_post_type_hierarchical('custom_type')) {
        $args['hierarchical'] = false;
    }

    register_post_type('custom_type', $args);
}
add_action('init', 'create_custom_post_type');

引用元: https://developer.wordpress.org/reference/functions/register_post_type/

サンプル3: メニューの生成

このコードは、階層的な投稿タイプを持つ場合にナビゲーションメニューを生成します。

function build_navigation() {
    $post_type = 'custom_type';
    if (is_post_type_hierarchical($post_type)) {
        // 階層的なメニューを生成する
        echo '<ul>' . wp_list_pages('title_li=&sort_column=menu_order&post_type=' . $post_type) . '</ul>';
    }
}

引用元: https://developer.wordpress.org/reference/functions/wp_list_pages/

サンプル4: カスタムクエリの作成

階層的な投稿を取得するためのカスタムクエリを作成します。

$args = array(
    'post_type' => 'custom_type',
    'posts_per_page' => -1,
    'orderby' => 'menu_order',
    'order' => 'ASC'
);
$query = new WP_Query($args);
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        if (is_post_type_hierarchical(get_post_type())) {
            // 階層的な表示
        }
    }
}

引用元: https://developer.wordpress.org/reference/classes/wp_query/

サンプル5: 権限の設定

このコードは、特定のユーザーロールに対して、階層的な投稿タイプへのアクセスを設定します。

function set_custom_post_type_capabilities() {
    $post_type = 'custom_type';
    if (is_post_type_hierarchical($post_type)) {
        $role = get_role('editor');
        $role->add_cap('edit_others_' . $post_type);
    }
}
add_action('admin_init', 'set_custom_post_type_capabilities');

引用元: https://developer.wordpress.org/reference/functions/get_role/

この関数のアクションでの使用可能性

アクション 使用可能性
mu_plugin_loaded
registered_post_type
plugins_loaded
wp_roles_init
setup_theme
after_setup_theme
set_current_user
init
register_sidebar
wp_loaded
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head

非推奨または削除されたバージョン

この関数は特定のバージョンで非推奨または削除された形跡はありません。

この関数について質問する


上の計算式の答えを入力してください