Managing WordPress Taxonomies for Better Usability

WordPress Taxonomies

Management of WordPress taxonomies and their uses have been discussed here.

WordPress offers a wonderful platform with amazing custom services.

This most popular CMS is used by the majority of users.

For the folks who are interested in creating custom posts, with this article you will be able to understand and deploy custom archive taxonomies.

Whether you want to create a custom taxonomy or you want to customize an existing one, here you will surely get an effective solution.

Taxonomy: A Foreword

The taxonomy refers to a content type in WordPress that is ideal for organizing the content, which possesses some other content type.

taxonomy

The most common taxonomies that most of you probably would be familiar with are Categories and Tags.

It is assumed that you are aware of both categories and tags in WordPress, these two integral taxonomies with hierarchical and non-hierarchical orientation respectively. Hierarchical Taxonomy means that it possesses a hierarchical structure with parent-child relationships and Non-hierarchical Taxonomy doesn’t exhibit any such relationship.

What WordPress does is, it creates an automated archive for each tag, category and custom taxonomy that follows a reverse chronological order and generates a list of associated posts. The site that features posts that are kept well organized with proper tags and categories are comparatively easier to maintain than it is to with custom posts and custom taxonomies.

In WP, the display for each archive is defined in template files, thus while moving towards customization, the very first step is to determine which template file is associated with your target.

How To Create Custom Taxonomies?

There are several ways of creating custom taxonomies, however, the most preferred one is by deploying a suitable plugin. There is no dearth of WP plugins, you can conveniently find an efficient plugin to accomplish the job with a greater proficiency.

However, while implementing any other method, it is recommended to create a new plugin for the cause instead of inserting the appropriate chunk of code to the functions.php file. Because, it will make the method less complex and will offer the added advantage in the future by making the created custom taxonomy easily available.

For creating a plugin, simply add the below mentioned line of code at the top of the text file boasting appropriate code.

/* Plugin name: Custom Taxonomy */

Here is the code snippet for a plugin that has been created to support custom taxonomy, which is named as “fruits”.

_x( 'TastyFruits', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'TastyFruit', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Taxonomy', 'text_domain' ),
'all_Fruits' => __( 'All Fruits', 'text_domain' ),
'parent_Fruit' => __( 'Parent Fruit', 'text_domain' ),
'parent_Fruit_colon' => __( 'Parent Fruit:', 'text_domain' ),
'new_Fruit_name' => __( 'New Fruit name', 'text_domain' ),
'add_new_Fruit' => __( 'Add new Fruit', 'text_domain' ),
'edit_Fruit' => __( 'Edit Fruit', 'text_domain' ),
'update_Fruit' => __( 'Update Fruit', 'text_domain' ),
'separate_Fruits_with_commas' => __( 'Separate Fruits with commas', 'text_domain' ),
'search_Fruits' => __( 'Search Fruits', 'text_domain' ),
'add_or_remove_Fruits' => __( 'Add or remove Fruits', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used Fruits', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
);
register_taxonomy( 'tastyfruit', array( 'post' ), $args );

}

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

}
?>

How To Customize Taxonomy Archives?

For those who are interested in using WP as a CMS and not just as a blogging tool, they often need to customize the built-in functions of WP, which is easily feasible with custom taxonomies.

By default, the WordPress implements the WP_Query class to automatically fetch the posts for the viewers while considering the page on which they are. However, this query can be tweaked for adding custom post types to tag or category archives, for this what you can do is, integrate the pre_get_posts filter.

Now, let’s see how it works.

The filter helps tweak the query when it is called before any post has been fetched by the WP. You can create arguments (“post” as a regular post and “latest” as a custom post type) to be passed in the WP_Query. By following the below mentioned code.

Code Snippet:

array(
‘post’,
‘latest’
)
);
?>

Here, for passing the changed post_type arguments we will need to implement the class method set(). As this method enables one to alter the arguments while the class has been already created.

Code Snippet:

is_category() && $query->is_main_query() ) {
$query->set( 'post_type',
array(
'post',
'latest'
)
);
}

return $query;

}
?>

By following the aforementioned guide thoroughly, you will be able to efficiently customize desired taxonomies archives in WordPress.

Author Bio :

Sophia Phillips has been working as a professional in WordPress web development company and loves sharing information about leveraging multiple benefits of WordPress in the best possible manner. Currently, she has an impressive count of WordPress -related articles under her name.

About Sonl Sinha

Sonl SinhaSonl Sinha is a passionate writer as well as WordPress and WooCommerce rockstar who loves to share insights on various topics through his engaging blog posts. He runs a successful website design and digital marketing company. With 15+ years of experience in WordPress theme development, he strives to inform and inspire readers with his thought-provoking content. He helps thousands of small and medium businesses and startups create a unique online presence. Follow Sonl Sinha for your regular dose of knowledge and inspiration.

Do check out our free WP themes and WordPress themes bundle