Explain how to add normal tag function to WordPress custom post and support archive display

update Last updated: February 3, 2024 at 1:52 PM

It was also a pending issue, but tags could not be used in news articles created with WordPress custom post types.
Therefore, we have modified the theme program and the WordPress system so that the tags that have been created for regular blogs can be used from news articles.
The title image is an example of adding a tag list with a link to the custom post list on the WP admin screen.

The following four points were addressed this time.

  • Add tagging functionality to custom post types
  • Add a tag item to the custom post list on the WP admin screen
  • Include custom posts in tag archive pages
  • Update permalink structure

The above functions can be realized without using any plugins, just by adding to the theme program functions.php and updating the permalink structure.
There is also a way to use the plugin "Custom Post Type UI", but in reality it is unexpectedly troublesome and sometimes it does not work well, so we recommend the method without using the plugin.

The details are described below.

Add tagging functionality to custom post types

Makes tags created in WordPress core available to articles in custom post types.
Add the following snippet to functions.php. It also supports block editor.
‘news’ and ‘gallery’ in the marked row are the taxonomies of custom post types (news, gallery).

* 2023.04.29 update
In order to prevent the tag archive URL from becoming “/post_tag/tag name/” due to changes in the permalink structure, use slug rewrite.is the original setting “/tag/tag name/” Modify the program so that it is displayed (add the parameter on line 9)

/* カスタム投稿タイプにタグ付け機能を追加する
---------------------------------------------------------------- */
add_action( 'init', function () {
		register_taxonomy( 'post_tag', [ 'post', 'news', 'gallery' ],
		[
			'hierarchical' => false,
			'query_var'    => 'tag',
			'show_in_rest' => true, // ブロックエディタ対応
			'rewrite' => array( 'slug' => 'tag' ), // *スラッグを更新 … 2023.04.29			
		]
	);
});
add_action('pre_get_posts', function ($query){
	if ( is_admin() && ! $query->is_main_query() ) {
		return;
	}
	if ( $query->is_category() || $query->is_tag() ) {
		$query->set('post_type', ['post','news','gallery']);
	}
});

Add a tag item to the custom post list on the WP admin screen

Allows tags to be displayed in the custom posts list in the WordPress admin screens.
We have prepared three types of scripts: a script that displays the tag list in a simple plain, a script that displays it with a link, and a script that displays it in a list format with a link!

(1) Script to plain display tag list of custom post

Add the following snippet to functions.php. The string ‘news’ in the marked line is the custom post type (news article) taxonomy.
This script displays a plain list of tags without links. The '#' prefix is ​​omitted.

/* カスタム投稿一覧(管理画面)にタグの項目を追加する
---------------------------------------------------------------- */
function add_tag_post_column_title( $columns ) {
	$columns[ 'slug' ] = "タグ";
	return $columns;
}
function add_tag_post_column( $column_name, $post_id ) {
	if( $column_name == 'slug' ) {
		$tags = get_the_tags();
		if($tags){
			foreach ( $tags as $tag ) {
				echo $tag->name .' ';
			}
		}
	}
}
add_filter( 'manage_news_posts_columns', 'add_tag_post_column_title' );
add_action( 'manage_news_posts_custom_column', 'add_tag_post_column', 10, 2 );

(2) Script to display custom post tag list with link

Add the following snippet to functions.php. The string ‘news’ in the marked line is the custom post type (news article) taxonomy.
This script displays a list of tags with links. A '#' prefix is ​​also added.

/* カスタム投稿一覧(管理画面)にリンク付きでタグの項目を追加する
---------------------------------------------------------------- */
function add_tag_post_column_title( $columns ) {
	$columns[ 'slug' ] = "タグ";
	return $columns;
}
function add_tag_post_column( $column_name, $post_id ) {
	if( $column_name == 'slug' ) {
		$posttags = get_the_tags();
		if( $posttags ){
			foreach ( $posttags as $tag ) {
				echo '<a href="' . get_tag_link( $tag->term_id ) . '">#' . $tag->name . '</a> ';
			}
		}
	}
}
add_filter( 'manage_news_posts_columns', 'add_tag_post_column_title' );
add_action( 'manage_news_posts_custom_column', 'add_tag_post_column', 10, 2 );

(3) Script to display tag list of custom post in linked list format

Add the following snippet to functions.php. The string ‘news’ in the marked line is the custom post type (news article) taxonomy.
This script displays a list of tags in linked list format. A '#' prefix is ​​also added.

/* カスタム投稿一覧(管理画面)にリンク付きのリスト形式でタグの項目を追加する
---------------------------------------------------------------- */
function add_tag_post_column_title( $columns ) {
	$columns[ 'slug' ] = "タグ";
	return $columns;
}
function add_tag_post_column( $column_name, $post_id ) {
	if( $column_name == 'slug' ) {
		$posttags = get_the_tags();
		if( $posttags ){
			echo '<ul class="tag-list">';
			foreach ( $posttags as $tag ) {
				echo '<li><a href="' . get_tag_link( $tag->term_id ) . '">#' . $tag->name . '</a></li>';
			}
			echo '</ul>';
		}
	}
}
add_filter( 'manage_news_posts_columns', 'add_tag_post_column_title' );
add_action( 'manage_news_posts_custom_column', 'add_tag_post_column', 10, 2 );

Include custom posts in tag archive pages

Include custom posts in the tag's archive page as well.
Add the following snippet to functions.php. The two marked lines, ‘news’ and ‘gallery’, are custom post type taxonomies.

/* タグのアーカイブページにカスタム投稿を含める
// 【注意】 functions.php を変更後、以下のパーマリンク構造の更新操作が必要
//  ☞ WP設定→ パーマリンク→ 「変更を保存」を選択
---------------------------------------------------------------- */
function add_post_tag_archive( $wp_query ) {
	if ($wp_query->is_main_query() && $wp_query->is_tag()) {
		$wp_query->set( 'post_type', array('post','news','gallery'));
	}
}
add_action( 'pre_get_posts', 'add_post_tag_archive' , 10 , 1);

Update permalink structure

In order to be able to use tags in custom posts, after changing functions.php, you need to update the permalink structure as follows.

WP Settings -> Permalinks -> Select "Save Changes"

2024.01.16 Added

Display categories in the custom post management screen list

As an example, the code to display "News Category" in the custom post (news) management screen list is as follows. This is an addition to functions.php.

/* 管理画面一覧にニュースのカテゴリを表示 */
function manage_news_columns( $columns ) {
	$columns['news_category'] = 'ニュースのカテゴリー';
	return $columns;
}

function add_news_column( $column_name, $post_id ){
	if ( 'news_category' == $column_name ) {
		$fcategory = get_the_term_list( $post_id, 'newscat', '', '' );
		if ( $fcategory ) {
			echo $fcategory;
		} else {
			echo __( 'None', '_hpb' );
		}
	}
}

add_filter( 'manage_edit-news_columns', 'manage_news_columns' );
add_action( 'manage_news_posts_custom_column', 'add_news_column', 10, 2 );

Reference article

Below are links to articles related to custom posts.

Add this entry to the hasebookmark
X (post)

Leave a Reply