Title: カスタム投稿タイプとカスタム分類のパーマリンクで404に
Last modified: 2016年11月6日

---

# カスタム投稿タイプとカスタム分類のパーマリンクで404に

 *  解決済 [kazaisyu](https://ja.wordpress.org/support/users/kazaisyu/)
 * (@kazaisyu)
 * [10年、 7ヶ月前](https://ja.wordpress.org/support/topic/%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e6%8a%95%e7%a8%bf%e3%82%bf%e3%82%a4%e3%83%97%e3%81%a8%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e5%88%86%e9%a1%9e%e3%81%ae%e3%83%91%e3%83%bc%e3%83%9e%e3%83%aa%e3%83%b3/)
 * ＵＲＬを
    /カスタム投稿/カスタム分類/投稿名（スラッグ） から /カスタム分類/投稿
   名（スラッグ） に変更したく、カスタム投稿名を抜けないか試行錯誤しています。 しかし、
   パーマリンクは変更できるものの、該当のページで404が発生してしまいます…。色々と
   弄ってはみたもののうまくいかない状態です。 どうか何卒、お力をお貸しいただけれ
   ば幸いです。
 *     ```
       add_action( 'init', 'create_post_type' );
       function create_post_type() {
       	register_post_type( 'article',
       	array(
       		'hierarchical'=> false,
       		'labels' => array(
       		'name' => __( '記事追加' ),
       		'singular_name' => __( '記事追加' )
       		),
       		'public' => true,
       		'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields' ,'comments' ),
       		'menu_position' =>5,
       	));
       	register_taxonomy('article-cat','article',
       			array(
             			'hierarchical' => true,
       				'label' => __('カテゴリ'),
       				'singular_label' => __('カテゴリ'),
       				'public' => true,
       				'show_ui' => true,
       			)
       	);
       }
       add_action( 'init', 'mytheme_add_rewrite_tag', 0 );
       function mytheme_add_rewrite_tag() {
        	global $wp_rewrite;
        	$queryarg = 'post_type=article&name=';
         	$wp_rewrite->add_rewrite_tag('%article_postname%', '([^/]+)', $queryarg);
         	$wp_rewrite->add_permastruct('article', '%article-cat%/%article_postname%', false);
       }
       add_filter('post_type_link', 'myposttype_permalink', 1, 3);
       function myposttype_permalink($post_link, $id = 0, $leavename) {
       	global $wp_rewrite;
       	$post = get_post($id);
       	if ( is_wp_error( $post ) ){
       		return $post;
       	}
       	if($post->post_type == 'article'){
       		$newlink = $wp_rewrite->get_extra_permastruct('article');
       		$newlink = str_replace('%article_postname%', $post->post_name, $newlink);
       		$newlink = home_url(user_trailingslashit($newlink));
       		return $newlink;
       	}else{
       		return $post_link;
       	}
       }
       add_filter( 'post_type_link', 'custom_term_link' , 1, 3 );
       function custom_term_link( $post_link, $id = 0 ) {
       	$post = get_post($id);
       	if ( is_wp_error($post) || 'article' != $post->post_type || empty($post->post_name) ){
       		return $post_link;
       	}
       	$terms = get_the_terms($post->ID, 'article-cat');
       	if( is_wp_error($terms) || !$terms ) {
       		$term_slug = 'uncategorised';
       	} else {
       		$term_obj = array_pop($terms);
       		$term_slug = $term_obj->slug;
       	}
       	return home_url(user_trailingslashit( "$term_slug/$post->post_name" ));
       }
       ```
   

2件の返信を表示中 - 1 - 2件目 (全2件中)

 *  [jim912](https://ja.wordpress.org/support/users/jim912/)
 * (@jim912)
 * [10年、 7ヶ月前](https://ja.wordpress.org/support/topic/%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e6%8a%95%e7%a8%bf%e3%82%bf%e3%82%a4%e3%83%97%e3%81%a8%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e5%88%86%e9%a1%9e%e3%81%ae%e3%83%91%e3%83%bc%e3%83%9e%e3%83%aa%e3%83%b3/#post-181751)
 * mytheme_add_rewrite_tag の優先順序が 0で指定されているのに対し、create_post_type
   の優先順序がデフォルトの10なので、後から実行される register_post_type でのパーマ
   リンク構造に上書きされてしまっています。
 * なお、この事象が解消されると、URLからカスタム投稿と固定ページや投稿（パーマリンク
   設定による）との判別がつかなくなるため、固定ページ、投稿の個別投稿ページが404
   になります。
 *  トピック投稿者 [kazaisyu](https://ja.wordpress.org/support/users/kazaisyu/)
 * (@kazaisyu)
 * [10年、 6ヶ月前](https://ja.wordpress.org/support/topic/%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e6%8a%95%e7%a8%bf%e3%82%bf%e3%82%a4%e3%83%97%e3%81%a8%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e5%88%86%e9%a1%9e%e3%81%ae%e3%83%91%e3%83%bc%e3%83%9e%e3%83%aa%e3%83%b3/#post-181834)
 * jim912さん、ありがとうございます。
    色々と考えてみたものの解決できそうになかっ
   たので、カスタム投稿を普通の投稿に（メニュー名を変えることでUIを改善）、それ以外
   をカスタム投稿に振り分けることで解決することにしました。

2件の返信を表示中 - 1 - 2件目 (全2件中)

 トピック「カスタム投稿タイプとカスタム分類のパーマリンクで404に」には新たに返信
することはできません。

## タグ

 * [カスタム分類](https://ja.wordpress.org/support/topic-tag/%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e5%88%86%e9%a1%9e/)
 * [カスタム投稿](https://ja.wordpress.org/support/topic-tag/%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e6%8a%95%e7%a8%bf/)
 * [パーマリンク](https://ja.wordpress.org/support/topic-tag/%e3%83%91%e3%83%bc%e3%83%9e%e3%83%aa%e3%83%b3%e3%82%af/)
 * [PHP](https://ja.wordpress.org/support/topic-tag/php/)

 * フォーラム: [使い方全般](https://ja.wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2件の返信
 * 2人の参加者
 * 最後の返信者: [kazaisyu](https://ja.wordpress.org/support/users/kazaisyu/)
 * 最後の活動: [10年、 6ヶ月前](https://ja.wordpress.org/support/topic/%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e6%8a%95%e7%a8%bf%e3%82%bf%e3%82%a4%e3%83%97%e3%81%a8%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e5%88%86%e9%a1%9e%e3%81%ae%e3%83%91%e3%83%bc%e3%83%9e%e3%83%aa%e3%83%b3/#post-181834)
 * 状態: 解決済

## トピック

### 人気のトピック

### 返信がないトピック

### サポート関連ではないトピック

### 解決済みのトピック

### 未解決のトピック

### すべてのトピック
