permalink

October 13, 2023

Table of Contents

カスタム投稿リライト

// 固定ページと衝突するからslug更新
function overwrite_custom_post_type_args( $args, $post_type ) {


$args['rewrite'] = array(
    'slug' => $post_type . 'mk2',
    'with_front' => false,
);

  return $args;
}
add_filter( 'register_post_type_args', 'overwrite_custom_post_type_args', 20, 2 );

リライト

archive

add_rewrite_rule(
    '^news2/?$',, 
    'index.php?post_type=news', 
    'top'
);

投稿詳細

// 投稿idリダイレクト
add_rewrite_rule(
    '^news/([0-9]+)?$',
    'index.php?post_type=news&p=$matches[1]', 
    'top'
);

// カスタムスラッグリダイレクト(階層のパーマリンクを独自カスタム)
add_rewrite_rule(
    '^hoge/news/([a-zA-Z0-9-_]+)?$',
    'index.php?post_type=news&name=$matches[1]', 
    'top'
);

custom pager

// archive pager
add_rewrite_rule(
    '^news/page/([0-9]+)?$',, 
    'index.php?post_type=news&paged=$matches[1]',
    'top'
);

rewrite rule class

<?php
class FW_Rewrite_Rule {
  public function __construct() {
    $this->custom_rule();
  }

  public function custom_rule() {

    add_action('init', function() {
      $post_type_list = fw_get_post_type_list();

      foreach ( $post_type_list as $post_type) {
        $this->rewrite_post_type($post_type);
      }
    });
  }

  private function rewrite_post_type($post_type) {
    $archive_url = fw_get_archive_url($post_type);
    $redirect_post_type = 'post_type=' . $post_type;

    // archive
    $this->rewrite_archive($archive_url, $redirect_post_type, $post_type);

    // 投稿詳細 募集要項のみ構造が違う
    if ($post_type == 'application') {
      $terms = get_terms('application-category', []);

      foreach ($terms as $term) {
        $term_slug = $term->slug;
        add_rewrite_rule(
          '^' . $archive_url . '/(' . $term_slug  . ')/([^/]+)/?$', 
          'index.php?' . $redirect_post_type . '&application=$matches[2]', 'top'
        );
      }
    } else {
      $this->rewrite_single_post($archive_url, $redirect_post_type);
    }
  }

  // add_rewrite_ruleの引数regex
  private function get_regex($regex_list, $archive_url) {
    $regex_parts = [
      'archive' => $archive_url,
      'pager' => 'page/([0-9]+)',
      'year' => 'date/([0-9]{4})',
      'month' => 'date/([0-9]{4})/([0-9]{2})',
      'date' => 'date/([0-9]{4})/([0-9]{2})/([0-9]{2})',
      'single_post' => '([0-9]+)',
      'single_post_custom' => '([a-zA-Z0-9-_]+)',
    ];

    $regex = '';

    foreach ($regex_list as $regex_label) {
      $regex .= $regex_parts[$regex_label] . '/';
    }

    return '^' . $regex . '?$';
  }


  // 投稿一覧
  private function rewrite_archive($archive_url, $redirect_post_type, $post_type) {
    // archive top
    add_rewrite_rule(
      $this->get_regex(['archive'], $archive_url), 
      'index.php?' . $redirect_post_type, 
      'top'
    );

    // archive pager
    add_rewrite_rule(
      $this->get_regex(['archive', 'pager'], $archive_url), 
      'index.php?' . $redirect_post_type . '&paged=$matches[1]',
      'top'
    );

    // rewrite category
    $this->rewrite_taxonomy(
      $archive_url, $redirect_post_type, 
      fw_get_category_slug($post_type)
    );

    // rewrite tags
    $this->rewrite_taxonomy(
      $archive_url, $redirect_post_type, 
      fw_get_tag_slug($post_type)
    );
    
    // date
    $this->rewrite_date_archive($archive_url, $redirect_post_type);
  }

  // 投稿詳細
  private function rewrite_single_post($regex_archive, $redirect_post_type) {
    add_rewrite_rule(
      $this->get_regex(['archive', 'single_post'], $regex_archive), 
      'index.php?' . $redirect_post_type . '&p=$matches[1]', 
      'top'
    );
    // スラッグをカスタムした際に親ページを指定してない場合は問題ないが、fw機能の親ページを指定した場合のパーマリンク
    add_rewrite_rule(
      $this->get_regex(['archive', 'single_post_custom'], $regex_archive), 
      'index.php?' . $redirect_post_type . '&name=$matches[1]', 
      'top'
    );
  }

  // custom taxonomy rewrite. category and tagをリライト
  private function rewrite_taxonomy($archive_url, $redirect_post_type, $taxonomy_slug) {

    $redirect_head = 'index.php?' . $redirect_post_type .  '&' . $taxonomy_slug . '=$matches[1]';

    $terms = get_terms($taxonomy_slug, []);
    
    // rewrite category 
    foreach ($terms as $term) {
      $term_slug = isset($term->slug) ? $term->slug : null;

      if (isset($term->slug)) {
        $regex_archive = $archive_url . '/(' . $term_slug . ')';

        // taxonomy archives top
        add_rewrite_rule(
          $this->get_regex(['archive'], $regex_archive), 
          $redirect_head, 
          'top'
        );

        // taxonomy archives pager
        add_rewrite_rule(
          $this->get_regex(['archive', 'pager'], $regex_archive), 
          $redirect_head . '&paged=$matches[2]', 'top'
        );

        $this->rewrite_date_archive($regex_archive, $redirect_post_type, $taxonomy_slug);
      }
    }
  }

  // 日付アーカイブス
  private function rewrite_date_archive($regex_archive, $redirect_post_type, $taxonomy_slug = null) {

    $redirect_head = 'index.php?' . $redirect_post_type;

    $redirect_year = '&year=$matches[1]';
    $redirect_year_pager = '&year=$matches[1]&paged=$matches[2]';
    $redirect_month = '&year=$matches[1]&monthnum=$matches[2]';
    $redirect_month_pager = '&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]';
    $redirect_date = '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]';
    $redirect_date_pager = '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]';

    if (isset($taxonomy_slug)) {
      $redirect_head .= '&' . $taxonomy_slug . '=$matches[1]';

      $redirect_year = '&year=$matches[2]';
      $redirect_year_pager = '&year=$matches[2]&paged=$matches[3]';
      $redirect_date = '&year=$matches[2]&monthnum=$matches[3]';
      $redirect_date_pager = '&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]';
      $redirect_date = '&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]';
      $redirect_date_pager = '&year=$matches[2]&monthnum=$matches[3]&day=$matches[4]&paged=$matches[5]';
    }

    // Yearly archives for a term.
    add_rewrite_rule(
      $this->get_regex(['archive', 'year'], $regex_archive), 
      $redirect_head . $redirect_year, 
      'top'
    );

    // Yearly archives for a term. and pager
    add_rewrite_rule(
      $this->get_regex(['archive', 'year', 'pager'], $regex_archive), 
      $redirect_head . $redirect_year_pager, 
      'top'
    );

    // month archives for a term.
    add_rewrite_rule(
      $this->get_regex(['archive', 'month'], $regex_archive),
      $redirect_head . $redirect_month, 
      'top'
    );

    // month archives for a term. and pager
    add_rewrite_rule(
      $this->get_regex(['archive', 'month', 'pager'], $regex_archive), 
      $redirect_head . $redirect_month_pager, 
      'top'
    );

    // Daily archives for a term.
    add_rewrite_rule(
      $this->get_regex(['archive', 'date'], $regex_archive),
      $redirect_head . $redirect_date, 
      'top'
    );

    // Daily archives for a term. and pager
    add_rewrite_rule(
      $this->get_regex(['archive', 'date', 'pager'], $regex_archive), 
      $redirect_head . $redirect_date_pager, 
      'top'
    );
  }
}

関連関数

add_filter - wp_unique_post_slug

投稿idをパーマリンク

add_filter('wp_unique_post_slug', function($slug, $post_ID, $post_status, $post_type, $post_parent) {
    // single_is_custom_slugがオフのとき
    if (true) {
        return $post_ID;
    }
    return $slug;
}, 10, 5);

add_filter - post_link

// デフォルト投稿パーマリンクカスタム デフォルト投稿 post type == post

add_filter( 'post_link', function($permalink, $post, $leavename) {
    $permalink = home_url('/' . 'blog' . '/' . $post->ID . '/');

    return $permalink;
}, 10, 3 );

add_filter - post_type_link

カスタム投稿

add_filter('post_type_link', function($url, $post) {
    return home_url('/' . $archive_url . '/' . $post->ID . '/');
}, 10, 2);

タームリンク

add_filter('term_link', function($url, $term, $taxonomy) {
    switch ($taxonomy) {
    case 'category':
    case 'post_tag':
        $url = home_url('/' . 'blog' . '/' . $term->slug . '/');
        break;
    case 'news-category':
    case 'news-tag':
        $url = home_url('/' . 'news'. '/' . $term->slug . '/');
        break;
    }
    return $url;
}, 10, 3);
function custom_date_link_for_taxonomy($url) {
  return $url;
}
add_filter('year_link', 'custom_date_link_for_taxonomy');
add_filter('month_link', 'custom_date_link_for_taxonomy');
add_filter('day_link', 'custom_date_link_for_taxonomy');