Developer // Adding Categories and Tags Functionality To WordPress Custom Post Types

Had a request from a client to add categories and tags to their resources custom post type in WordPress.

Obviously I assumed there were predefined WP functions to implement this, but I found it hard to pin down a complete solution, so thought I would share mine here.

Firstly the custom post type we want to add categories and tags to, ‘resources’ in this case.
register_post_type('resources',
array(
'labels' => array(
'name' => 'Resources',
'singular_name' => 'Resource',
'add_new' => 'Add New',
'add_new_item' => 'Add New Resource',
'edit' => 'Edit',
'edit_item' => 'Edit Resource',
'new_item' => 'New Resource',
'view' => 'View',
'view_item' => 'View Resource',
'search_items' => 'Search Resources',
'not_found' => 'No Resources found',
'not_found_in_trash' => 'No Resources found in Trash',
'parent' => 'Parent Resources'
),

'public' => true,
'menu_position' => 43,
'supports' => array('title', 'editor', 'thumbnail'),
'taxonomies' => array(),
'menu_icon' => 'dashicons-admin-page',
'has_archive' => true
)
);

Add Taxonomies

Next we need to add inĀ 2 new taxonomy functions creating the category and tag taxonomies to associate with our ‘resources’ post type.

This involves setting up the taxonomy as usual, then using the wordpress ‘register_taxonomy()’ function to register the new category and tags taxonomies against our ‘resources’ custom post type.

Categories

function create_resources_taxonomies() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category Name' ),
'menu_name' => __( 'Categories' ),
);

$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'categories' ),
);

register_taxonomy( 'resources_categories', array( 'resources' ), $args );
}

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

Tags

function create_resources_tags_taxonomies() {
$labels = array(
'name' => _x( 'Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
'search_items' => __( 'Search Tags' ),
'all_items' => __( 'All Tags' ),
'parent_item' => __( 'Parent Tag' ),
'parent_item_colon' => __( 'Parent Tag:' ),
'edit_item' => __( 'Edit Tag' ),
'update_item' => __( 'Update Tag' ),
'add_new_item' => __( 'Add New Tag' ),
'new_item_name' => __( 'New Tag Name' ),
'menu_name' => __( 'Tags' ),
);


$args = array(
'hierarchical' => false, // Set this to 'false' for non-hierarchical taxonomy (like tags)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'tags' ),
);

register_taxonomy( 'resources_tags', array( 'resources' ), $args );
}

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

The only difference to note here, between the category and tags taxonomies, is that the ‘hierarchical’ argument is true for categories, but must be set to false for tags.

JSON File Query

 

if($categoryid != '' && $tagid != ''){
$args = array(
'posts_per_page' => 20,
'offset' => $offset,
'orderby' => 'date',
'post_type' => 'resources',
's' => $searchterm,
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'resources_categories',
'field' => 'id',
'terms' => $categoryid
),
array(
'taxonomy' => 'resources_tags',
'field' => 'id',
'terms' => $tagid
)
)
);
} else {
$args = array(
'posts_per_page' => 20,
'offset' => $offset,
'orderby' => 'date',
'post_type' => 'resources',
's' => $searchterm,
'order' => 'DESC',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'resources_categories',
'field' => 'id',
'terms' => $categoryid
),
array(
'taxonomy' => 'resources_tags',
'field' => 'id',
'terms' => $tagid
)
)
);
}

$wp_query = new WP_Query($args);

while($wp_query->have_posts()) {
$wp_query->the_post();
//work with post data to output
});

Template Query

Category Output

$taxonomy = 'resources_categories';
$args = array(
'hide_empty' => 0
);

$categories = get_terms($taxonomy, $args);
foreach($categories as $category) { ?>

<li>
<span class="link" data-categoryid="<?php echo $category->term_id; ?>"><?php echo $category->name; ?></span>
</li>
<?php } ?>

 

Tag Output
<?php

$taxonomy = 'resources_tags';

$args = array( 'hide_empty' => 0,
'order' => 'ASC'
);

$tags = get_terms($taxonomy, $args);
foreach($tags as $tag) { ?>
<
?php echo $tag->name; ?>
<li>
<span class=”link light” data-tagid=”<?php echo $tag->term_id; ?>”><?php echo $tag->name; ?></span>
</li>
<?php } ?>

 

Leave a Reply

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