How Do I Create a Custom Taxonomy in WordPress Programmatically?

While there are several popular WordPress taxonomies such as tags, categories, and posts, many users may want to create their own custom taxonomy. This can be done programmatically using the wp_tax_query() function.

To create a custom taxonomy, you will first need to create a new WordPress object using the wp_create_object() function. This object will contain the information needed to create the taxonomy. The following example creates a new taxonomy called “custom_taxonomies”:

wp_create_object( ‘custom_taxonomies’, // required name of the taxonomy );

Next, you will need to specify the name of the taxonomy and the name of the object that will hold the taxonomy data. The following example sets the name of the taxonomy to “custom_taxonomies” and the name of the object to “custom_taxonomy_object”:

wp_create_object( ‘custom_taxonomies’, // required name of the taxonomy ‘custom_taxonomy_object’ // required name of the object that will hold the taxonomy data );

Finally, you will need to specify the taxonomy ID for the new taxonomy. You can find the taxonomy ID for a given taxonomy by using the wp_get_taxonomy() function. The following example returns the taxonomy ID for the “tags” taxonomy:

wp_get_taxonomy( ‘tags’ );

You can use the wp_tax_query() function to create a query that will return all posts in the “tags” taxonomy. The following example sets up the wp_tax_query() function to return all posts in the “tags” taxonomy with the ID of “1”:

wp_tax_query( ‘tags’, // required taxonomy name ‘1’ // required post ID );

The wp_tax_query() function will return the results of the query as an array of WP_Taxonomy objects. The following example prints out the results of the wp_tax_query() function:

printf( ‘%s

’, wp_tax_query( ‘tags’, ‘1’ ) );

The wp_tax_query() function will return the results of the query.