How to add tags to custom post type?

Most WordPress developers, when they should be use something like default tags of posts, but for custom post types register a new taxonomy. However, this approach is not comfortable, at least because at start we should be register tags-terms, and then use it in a posts. In case use default tags, they can create directly from post.

Adding tags to custom post type

Problem: Can add default tags to custom post type? And how to do it?

Solution: Here we wouldn't talk about classic way, for example some like this:

add_action( 'init', 'list_taxonomies' );
	
function list_taxonomies() {
		
	$labels = [
		'name' => _x( 'Tags', 'Taxonomy plural name', 'helloadmin' ),
		'singular_name' => _x( 'Tag', 'Taxonomy singular name', 'helloadmin' ),
		'menu_name' => __( 'Tags', 'helloadmin' ),
	];
		
	$args = [
		'hierarchical' => true,
		'labels' => $labels,
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => [ 'slug' => 'tags' ],
	];
		
	register_taxonomy( 'media-tag', [ 'media' ], $args );
		
	unset( $labels );
	unset( $args );
}

Step 1: Firstly we should be add string 'taxonomies' => ['post_tag'] to arguments of the function register post type.

add_action( 'init', 'list_post_type', 0 );

function list_post_type() {
    $labels = array(
        'name'                => __( 'Media Posts', 'helloadmin' ),
        'singular_name'       => __( 'Media Post', 'helloadmin' ),
        'menu_name'           => __( 'Media Posts', 'helloadmin' ),
        'parent_item_colon'   => __( 'Parent Item:', 'helloadmin' ),
        'all_items'           => __( 'All Media Posts', 'helloadmin' ),
        'view_item'           => __( 'View Item', 'helloadmin' ),
        'add_new_item'        => __( 'Add New Item', 'helloadmin' ),
        'add_new'             => __( 'Add New', 'helloadmin' ),
        'edit_item'           => __( 'Edit Item', 'helloadmin' ),
        'update_item'         => __( 'Update Item', 'helloadmin' ),
        'search_items'        => __( 'Search Item', 'helloadmin' ),
        'not_found'           => __( 'Not found', 'helloadmin' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'helloadmin' ),
    );
    $args = array(
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'page-attributes', 'author', 'comments' ),
        'taxonomies'          => ['post_tag'],
	    'show_in_rest'        => true,
        'hierarchical'        => false,
        'public'              => true,
        'menu_position'       => 5,
        'has_archive'         => true,
        'menu_icon'           => 'dashicons-media-video',
        'rewrite' => array(
            'slug' => 'media',
            'with_front' => true,

        ),
    );
    register_post_type( 'media-post', $args );

    unset($labels);
    unset($args);
}

At now we can set tags from a post, but we have one more problem. On the tag page we can't see our posts that have this tag. For resolve it, we should be change default loop on the tag's page.

Step 2: Add our custom post type media-post.

// Change Default Query On Tags Page Add CPT media-post
add_action( 'pre_get_posts', 'add_cpt_tags' );
	
function add_cpt_tags( $query ) {
	if ( ! is_admin() && $query->is_tag() && $query->is_main_query() ) {
		$query->set( 'post_type', [ 'post', 'media-post' ] );
	}
}

In case with default post categories we should be replace 'taxonomies' => ['post_tag'] to 'taxonomies' => ['category'] on the first step. And on second step replace is_tag() to is_category().

Source:

  1. https://wordpress.org/support/topic/adding-tags-to-custom-post-type/

Leave Comment

Your email address will not be published. Required fields are marked *