シンプルなリライトルール設定

例示している程度のURL構造であれば、既存のプラグインを使った方が早い。
Custom Post Type Permalinks ただし、最近の WordPress ではテストされていない。

カスタム投稿タイプの URL に ID の数字を使う設定

デフォルトはスラッグの文字列である。投稿 ID で表示したい場合の設定方法。

// 投稿タイプを登録
function my_post_type() {
    register_post_type(
        'news',
        array(
            'public' => true,
            'has_archive' => true,
            'rewrite' => array(
                             'with_front' => false,
                             'slug' => 'my-post-type'
            )
        )
    );
}
add_action( 'init', 'my_post_type' );

// /my-post-type/1234/ で投稿単体を表示するリライトルールを設定
function add_mypostype_rewrite_tag( $post_type ){
    if('my_post_type' === $post_type) {
        add_rewrite_tag('%my_post_type_id%', '([0-9]+)', 'post_type=my_post_type&p=');
        add_permastruct('my_post_type_id', 'my-post-type/%my_post_type_id%' );
        // ↑ 第一引数を投稿タイプと同じにすると追加されない
    }
}
add_action('registered_post_type', 'add_mypostype_rewrite_tag', 10, 1 );

// the_permalink() などで出力されるURLを変換
function my_post_type_link( $link, $post ){
    if ( 'my_post_type' !== $post->post_type ) {
        return $link;
    }

    return home_url( '/my-post-type/' . $post->ID );
}
add_filter( 'post_type_link', 'my_post_type_link', 1, 2 );

my_post_type_id という名前でパーマリンク構造を指定する。
既存のパーマリンク構造名と重複しない名前にする。投稿タイプ名である my_post_type は投稿タイプ生成時にデフォルトで生成されるため使えない。

余談ながら、WordPress のコーディングルールからヨーダ記法を廃止する提案がなされている。Proposal: Disallow assignments in conditions and remove the Yoda condition requirement for PHP – Make WordPress Core

投稿タイプに付与するタクソノミーのリライト設定

カスタム投稿タイプに専用タクソノミーを設定し、/<custom_post_type>/<term>/ としたい場合。

register_taxonomy(
    'my_taxonomy',
		array(
				'public'          => true,
				'show_ui'         => true,
				'show_in_rest'    => true,
		    'capability_type' => 'my_post_type',
		    'rewrite'         => false,
     )
);
		             
add_action( 'init', function(){ 
    add_rewrite_tag( '%my_type_tax%','([^/]+', 'post_type=my_custom_type&my_taxonomy=' );
    add_permastruct( 'my_custom_type_term', '/my-post-type/%my_type_tax%', array( 'with_front'=>false));
}, 10)

/my-post-type/([^/]+)(?:/([0-9]+))?/?$ リライトルールがマッチ範囲が広いが、シングルページのページ送り用のリライトルールのため、 my_post_type=term&page= が実行され 404 を返す。

/<custom_post_type>/my-taxonomy/<term>/ としたい場合は、'rewrite'=>'my-post-type/my-taxnomy/ と設定する。

add_permastruct は 設定 → パーマリンク → パーマリンク構造→カスタム構造 で設定するのと同じである

オプションで、ページネーションをカバーするか選択できる(デフォルトではカバーする)ので、ページネーションのためのリライトルールを書かなくてよい。逆にページネーションのURLを WordPress デフォルトから変えたい場合は使えない、add_rewrite_rule を使う。

add_permastruct() | Function | WordPress Developer Resources

add_rewrite_rule() | Function | WordPress Developer Resources

リライトルールの順序

リライトルールは概ね下記のグループである。グループごとにフィルターがある。
Rewrite API – WordPress Codex 日本語版

照合される順序

  1. wp-sitemap.xml
  2. カスタム投稿タイプのアーカイブ関係
    • 投稿1タイプごとに
      1. フィード
      2. RSS
      3. ページネーション
  3. デフォルトのカテゴリー
  4. デフォルトのタグ
  5. add_permalink で挿入される位置
  6. register_post_type / register_taxonomy で登録される投稿タイプ・タクソノミー
    • カスタム投稿タイプのシングルページ関係とタクソノミーのグループ。
      順序はコードを記述した順
  7. 固定ページ
  8. robots.txt / favicon.ico
  9. feed
  10. comment
  11. search
  12. 日付アーカイブ
  13. ルート向け

add_rewrite_rule は、このリライトルール配列の前に足すか、後ろに足すかである。

その他

無名関数だと remove_action() で外せなくなるが、リライトルールを他の処理で外すことは、まずないので無名関数でもいい。

add_action( 'init', function(){ ... }) 

下記コードが基本形、 my_type_tax というリライトタグは何の文字列を検出し、どんなクエリーを実行するか指定する。第2引数は正規表現、下記はスラッシュ以外の文字列にマッチする。この組み合わせで、投稿タイプが my_post_type かつ、正規表現でマッチした文字列のタームに属する投稿を探して表示する。

add_rewrite_tag( '%my_type_tax%', '([^/]+)', 'post_type=my_custom_type&my_taxonomy=' );
 add_permastruct( 'my_custom_type_term', '/my-post-type/%my_type_tax%',
									 array( 'with_front' => false));

リライトルールの設定は、init アクションや、add_rewrite_rule でもいい。

コード例を見たことがないが {$permastructname}_rewrite_rules フィルターでリライトを追加する記述も妥当そうである。デフォルトでは使わないリライトルールを削除して、必要なルールを追加して返すこともできる。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です