Adding Custom Taxonomy Filters to WordPress Plugin

Posted on 17th June 2023

Introduction

In this article, we will discuss how to add custom taxonomy filters to a WordPress plugin. We will be using the example of a plugin that displays a list of products and allows users to filter the products by category.

Creating the Plugin

First, we need to create a new WordPress plugin. For this example, we will name the plugin “My Plugin”. The plugin will have two files: my-plugin.php and my-plugin-admin.php.

my-plugin.php

my-plugin.php will be the main plugin file and will contain the code for displaying the products.

my-plugin-admin.php

my-plugin-admin.php will be the admin file and will contain the code for adding the custom taxonomy filters to the plugin.

Adding the Products

In order to display the products, we need to first add them to the WordPress database. We can do this by creating a custom post type for products.

Creating the Custom Post Type

Adding a custom post type is a two-step process. First, we need to register the post type, and then we need to add the products.

Registering the Post Type

We can register the post type by adding the following code to my-plugin.php:


function my_plugin_register_post_type() {

$labels = array(
'name' => 'Products',
'singular_name' => 'Product',
'add_new' => 'Add New Product',
'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 Trash',
'parent_item_colon' => '',
'menu_name' => 'Products'

);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'product' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' )
);

register_post_type( 'product', $args );
}
add_action( 'init', 'my_plugin_register_post_type' );

Adding the Products

Once the post type is registered, we can add the products. We can do this by going to the WordPress admin area and adding a new “Product” post.

Displaying the Products

Now that we have added the products, we can display them on the front-end of the site. We can do this by adding the following code to my-plugin.php:


function my_plugin_display_products() {

$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();

the_title();
the_content();

endwhile;
}
add_shortcode( 'my_plugin_products', 'my_plugin_display_products' );

This code will display a list of all the products on the front-end of the site.

Adding the Custom Taxonomy Filters

Now that we have the products displaying on the front-end of the site, we can add the custom taxonomy filters. We can do this by adding the following code to my-plugin-admin.php:


function my_plugin_admin_init() {

add_action( 'restrict_manage_posts', 'my_plugin_restrict_manage_posts' );
}
add_action( 'admin_init', 'my_plugin_admin_init' );

function my_plugin_restrict_manage_posts() {

global $typenow;

if ( 'product' == $typenow ) {

$taxonomies = array( 'product_cat' );

foreach ( $taxonomies as $taxonomy ) {

$selected = isset( $_GET[$taxonomy] ) ? $_GET[$taxonomy] : '';
wp_dropdown_categories( array(
'show_option_all' => 'Show All ' . $taxonomy,
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => false,
) );
}
}
}

Conclusion

In this article, we have discussed how to add custom taxonomy filters to a WordPress plugin. We have created a plugin that displays a list of products and allows users to filter the products by category. We have also added the code for adding the custom taxonomy filters to the plugin.

Adding Custom Taxonomy Filters to WordPress Plugin

In this article, we will learn how to add custom taxonomy filters to our WordPress plugin.

As we know, WordPress is a very flexible CMS and it allows us to create custom post types and custom taxonomies. Custom taxonomies are a great way to group our content and make it more organized.

Sometimes, we need to display only certain items from a custom taxonomy on our website. For example, we may want to show only the products from a certain category on our shop page.

In order to do this, we need to add a custom taxonomy filter to our WordPress plugin.

Adding a custom taxonomy filter to our plugin is very easy. We just need to add a few lines of code to our plugin file.

First, we need to register our custom taxonomy. We can do this by adding the following code to our plugin file:

function my_plugin_register_taxonomy() {

$labels = array(

‘name’ => ‘My Taxonomy’,

‘singular_name’ => ‘My Taxonomy’,

‘add_new’ => ‘Add New Taxonomy’,

‘add_new_item’ => ‘Add New Taxonomy’,

‘edit_item’ => ‘Edit Taxonomy’,

‘new_item’ => ‘New Taxonomy’,

‘view_item’ => ‘View Taxonomy’,

‘search_items’ => ‘Search Taxonomies’,

‘not_found’ => ‘No taxonomies found’,

‘not_found_in_trash’ => ‘No taxonomies found in Trash’,

);

$args = array(

‘labels’ => $labels,

‘public’ => true,

‘hierarchical’ => true,

‘show_ui’ => true,

‘show_in_menu’ => true,

‘show_in_nav_menus’ => true,

‘show_in_rest’ => true,

‘rest_base’ => ‘my-taxonomy’,

‘rest_controller_class’ => ‘WP_REST_Terms_Controller’,

);

register_taxonomy( ‘my-taxonomy’, ‘post’, $args );

}

add_action( ‘init’, ‘my_plugin_register_taxonomy’ );

In the above code, we have registered our custom taxonomy with the name “my-taxonomy”. We have also set the “public” argument to true so that our taxonomy will be visible to everyone.

Next, we need to add the following code to our plugin file:

function my_plugin_rest_prepare_taxonomy( $data, $request, $taxonomy ) {

$data->data[‘my-taxonomy’] = get_terms( array(

‘taxonomy’ => ‘my-taxonomy’,

‘hide_empty’ => false,

));

return $data;

}

add_filter( ‘rest_prepare_taxonomy’, ‘my_plugin_rest_prepare_taxonomy’, 10, 3 );

In the above code, we are using the “rest_prepare_taxonomy” filter hook to add our custom taxonomy terms to the REST API response.

Now, we can access our custom taxonomy terms in the REST API response.

Lastly, we need to add the following code to our plugin file:

function my_plugin_rest_taxonomy_query( $args, $request ) {

if ( isset( $request[‘my-taxonomy’] ) && !empty( $request[‘my-taxonomy’] ) ) {

$args[‘tax_query’] = array(

array(

‘taxonomy’ => ‘my-taxonomy’,

‘field’ => ‘slug’,

‘terms’ => $request[‘my-taxonomy’],

),

);

}

return $args;

}

add_filter( ‘rest_taxonomy_query’, ‘my_plugin_rest_taxonomy_query’, 10, 2 );

In the above code, we are using the “rest_taxonomy_query” filter hook to modify the default taxonomy query.

We are adding a new “tax_query” argument to the query. This “tax_query” argument will filter the posts by our custom taxonomy terms.

Now, we can access our custom taxonomy terms in the REST API response.

We hope this article helped you learn how to add custom taxonomy filters to your WordPress plugin.