Create multiple custom post type with category and tags in WordPress

0
820

In WordPress how to create common categories tags for multiple custom post types

 Plugin developers and website owners can design their own content kinds in WordPress. These custom post types, like posts and pages, can have their own taxonomies, such as categories and tags. If you write movie reviews, for example, you might wish to develop a new post type called ‘Movies.’

You may also need to categorize your movies into appropriate categories, which will necessitate the use of a custom taxonomy. You can make your own genre taxonomy.

What if you wanted to use the same categories for the Movies post type as you do for your blog posts?

You’ll need to allow categories for your custom post type at this point. This allows you to utilize the same categories for your blog posts and custom post types. After that, let’s look at how to quickly add categories to a custom post type in WordPress.

In this first of two posts, I’ll go through the three categories of custom content available in WordPress and how to generate them. I’ll go over:

Types of custom posts

Taxonomies that are unique to you

Fields that are unique to you.

I’ll introduce the notion of custom content in WordPress and show how to register and use a custom post type, as well as look at how WordPress displays posts created with that custom post type, in this first part of a two-part post series.

Types of Custom Posts

By default, WordPress provides you with a set of post kinds. These are the following:

  • Posts sPages
  • Attachments
  • Menu elements in the navigation menu
  • Links to previous revisions (if you’re utilizing the blogroll function)

Taxonomies that are unique to you

Custom taxonomies, like custom post types, add to the taxonomies already available in WordPress. These are the following:

Post formats Categories Tags

Link categories (which, if you’re using one, apply to links in your blogroll)

You can also make your own custom taxonomy to better organize your material. It’s vital to understand that a custom taxonomy is not a category and has no relation to categories; rather, the term “category” refers to one of WordPress’ taxonomies.

When you need to construct a hierarchical category structure, for example, the inbuilt categories can sometimes provide everything you need to categorize your information. However, this isn’t always adequate, especially if you want to be able to display posts from multiple taxonomies or if you’re utilizing a custom post type with its own taxonomy.

Here are several examples:

Product taxonomies – may be numerous ones, such as color, size, and fabric for a clothes store – on an e-commerce site.

Taxonomies for event types, venues, and more on an events listings site.

Taxonomies are used in a knowledgebase to sort data in a crosscutting manner: for example, in a web development knowledgebase, taxonomies for programming languages, level of complexity, and platforms might be used.

Because the wp insert post() function has been there since WordPress 1.0, you could simply set the post type value when inserting a post when the post type field was implemented in 1.5. Of course, generating and managing custom post types necessitated a lot more coding, but as WordPress functions became more flexible, the amount of scripting necessary decreased.

The register post type() method and a few other useful features were added to the nightly builds in version 2.8, and the functions were made available to everyone in version 2.9. Extensive code and hacks were no longer required to turn WordPress into a full-fledged CMS; instead, you could use a variety of wonderful built-in capabilities to make WordPress do your bidding.

Creating Custom Post Types

The first item on our to-do list is to create the post type itself. When working with custom post types, it’s best to create a plugin, but if you don’t know how or just need a quick test, you can use your theme’s

functions.php file. function my_custom_post_product() { $args = array(); register_post_type( ‘product’, $args ); } add_action( ‘init’, ‘my_custom_post_product’ );  

It will build a post type with almost no customization in its most basic version. It won’t be visible to the public, won’t appear in the admin, and interaction messages will be the same as for posts (“post saved,” “post updated,” etc.). I’ll go over some of the most commonly-used variables and add them to the previously empty $args array to customize our new post type to our needs.

function my_custom_post_product() { $labels = array( ‘name’ => _x( ‘Products’, ‘post type general name’ ), ‘singular_name’ => _x( ‘Product’, ‘post type singular name’ ), ‘add_new’ => _x( ‘Add New’, ‘book’ ), ‘add_new_item’ => __( ‘Add New Product’ ), ‘edit_item’ => __( ‘Edit Product’ ), ‘new_item’ => __( ‘New Product’ ), ‘all_items’ => __( ‘All Products’ ), ‘view_item’ => __( ‘View Product’ ), ‘search_items’ => __( ‘Search Products’ ), ‘not_found’ => __( ‘No products found’ ), ‘not_found_in_trash’ => __( ‘No products found in the Trash’ ), ‘parent_item_colon’ => ’, ‘menu_name’ => ‘Products’ ); $args = array( ‘labels’ => $labels, ‘description’ => ‘Holds our products and product specific data’, ‘public’ => true, ‘menu_position’ => 5, ‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’, ‘excerpt’, ‘comments’ ), ‘has_archive’ => true, ); register_post_type( ‘product’, $args ); } add_action( ‘init’, ‘my_custom_post_product’ );

Labels: The labels option should be an array that defines the various labels a custom post type can have. I separated this out earlier to make the case for registering a post type more explicit.

description A brief description of our unique post type, including what it does and why we use it.

Public: This option manages a number of things at once. If you set this to true, a slew of additional options (all related to visibility) will also be true. It is possible, for example, to make a custom post type visible but not queryable. There will be more on this later.

Menu position: The position of the custom post type menu in the back end is defined here. It will be put below the “posts” menu if you set it to “5”; the higher you set it, the lower the menu will be.

Supports: This option configures the default WordPress controls available in the custom post type’s edit screen. Only the title field and the editor are visible by default.

If you wish to support comments, revisions, post formats, and other features, you’ll need to add them here. Check out Codex’s arguments section for a complete list.

has archive: If true, rewrite rules will be generated for you, allowing you to archive post types at https://www.softwarequery.com//posttype/. (as a rule).

softwarequery.com-post type

PERSONALIZED INTERACTION MESSAGES :

WordPress sends out a variety of messages in response to user actions. In the back end, updating, publishing, searching, and so on all result in messages that are, by default, customized to regular posts. Using the post_updated_messages hook, you can quickly edit the text of these messages.

 

CONTEXTUAL HELP:

Customized contextual help is a feature I rarely see implemented. I’ve never used this feature as a user, but I’m sure many others have; in any event, it’s wonderful to provide some assistance to less experienced users.

When available, the contextual help function appears as a descending tab in the top right corner of pages. Let’s have a look at how you can update the contents.

 

Custom Taxonomies 

The organizational structure of your regular blog entries is created using categories and tags. For bespoke post kinds, however, the same organization does not always make sense. You could write about your “Life,” “Thoughts,” or “Dreams” in your blog postings. These are obviously unsuitable for use in products.

This is the issue that prompted developers to design custom taxonomies in the first place. You can establish a distinct taxonomy called “Product Categories” to house product-specific categories. I will only go into small detail here because Kevin Leary created a fantastic essay about custom taxonomies in WordPress, which I highly suggest.

 

You can create a taxonomy in the same way that you can create custom post types, but you’ll need to tweak it a little to make it fit your needs. Because custom taxonomies are public by default, they behave a little better out of the box, therefore the above is actually enough to link this taxonomy to the product posts. Let’s take a look at a specific example.

As you can see, there hasn’t been much of a change. We changed the hierarchical option to true and added some labels. This supports taxonomies in the “category style.” If false (the default), your taxonomy will be the same as the default tags. There are a couple more power alternatives available, which you may learn about in Leary’s article or by going to the website.

Meta Boxes should be posted

The draggable boxes you see in the WordPress edit screen for a post are known as meta boxes. There are several built-in meta boxes, such as publishing controls, taxonomies, and author boxes, but you can also design your own.

Meta boxes are far more user-friendly than the built-in custom fields box when it comes to managing custom field data. You may now add client-side error checking and a variety of other features now that the controls are in place.

Creating a meta box requires three steps:

  • Define the box itself,
  • Define the content of the meta box,
  • Define how the data from the box is handled
 

 

The code above produces a meta box with the parameters shown below (in the order given):

  1. The meta box’s unique identifier (it does not have to match the function name),
  2. The meta box’s title is (visible to users),
  3. The box’s contents will be displayed using this function.
  4. The post type to which the meta box relates
  5. The meta box’s positioning,
  6. The meta box’s priority (which defines “how high” it is placed)

MANAGEMENT OF SUBMITTED DATA

 In most circumstances, you’ll want to save the data as a custom field, although this isn’t the only option. You might utilize the input to call a third-party API, create an XML file, or whatever else you want. Let’s look at how to save custom post data, which is the most popular use case.
 
 

 

POSTS ARE DISPLAYED

If you set the has archive parameter to “true” when creating a post type, WordPress will list your posts on the archive page for that post type. If your post type is “books,” you can access your post list by going to http://mysite.com/books/.
 
If archive-[post type].php exists, it is used for the display on this page (archive-books.php in our case). If it doesn’t exist, archive.php will be used, and if that doesn’t exist, index.php will be used.
 
Using a custom query using the WP Query class is another option to display custom post-type content. You could implement something like this to show posts from a specific post type and custom taxonomy:
 
   <?php     $args = array(       'post_type' => 'product',       'tax_query' => array(         array(           'taxonomy' => 'product_category',           'field' => 'slug',           'terms' => 'boardgames'         )       )     );     $products = new WP_Query( $args );     if( $products->have_posts() ) {       while( $products->have_posts() ) {         $products->the_post();         ?>          </p><br />
<h1><?php the_title() ?></h1><br />
<div>            <?php the_content() ?>          </div><br />
<p>        <?php       }     }     else {       echo 'Oh ohm no products!';     }   ?>

LEAVE A REPLY

Please enter your comment!
Please enter your name here