0

Creating Custom Taxonomies Using the ACF Plugin: A Step-by-Step Guide

How to Create Custom Taxonomies with ACF Plugin 2

Taxonomies play a crucial role in organizing and categorizing the content on your website. Luckily, with the ACF plugin, you have the ability to create and assign custom taxonomies to any post type. In this blog post, we will unleash the potential of the ACF plugin by walking you through the process of creating a custom taxonomy in ACF, assigning a custom field group to it, and importing values for your taxonomy.

The Importance of Custom Taxonomies

Before we dive into the steps for creating custom taxonomies with the ACF plugin, let’s take a moment to understand why custom taxonomies are valuable for your website. While WordPress offers built-in taxonomies like categories and tags, these might not always align with your specific requirements.

In such cases, custom taxonomies allow you to classify your content in a way that better suits your needs. They provide a structured organizational system and offer flexibility in categorizing your content based on your unique requirements.

How to Create Custom Taxonomies

To begin creating custom taxonomies with ACF, make sure you have the plugin installed on your website. Once installed, you will see the ACF menu listed in the sidebar. Follow the steps below to get started:

  1. Navigate to ACF >> Taxonomies and click on “Add New.” This will take you to the “Add New Taxonomy” page.
  2. Fill in the required fields to configure the basic settings. Provide the plural label, singular label, and taxonomy key. The taxonomy key field is automatically generated based on the singular label, but you can edit it if desired.
  3. From the “Post Types” field, select the post types to which you want to assign this custom taxonomy. You can choose multiple post types.
  4. By default, the taxonomy will be visible on the front end. If you want the taxonomy to have hierarchical descendants, enable the “Hierarchical” toggle button.

Additional Settings Configuration

ACF simplifies the configuration process with additional settings that you can access by toggling the bar. Here’s a brief overview of the available tabs:

General: Enable the “Sort Terms” option to sort the taxonomy terms. If you have the default term enabled, provide the term name (mandatory), term slug, and term description. You can also provide a general description of the taxonomy. The “Active” toggle is enabled by default, but you can deactivate the taxonomy without deleting it.

Labels: Define the labels for the taxonomy, which will be used throughout the admin dashboard. The labels are automatically generated based on the singular and plural labels you configured in the basic settings, but you can edit them if necessary.

Visibility: Control the visibility of the taxonomies on the front and admin dashboards.

URLs, permissions, and REST API: These settings are preconfigured, but you can make changes based on your specific needs. For more detailed information, refer to the ACF official documentation on custom taxonomy.

Once you have configured the settings, click “Save Changes” to save the custom taxonomy in ACF.

Adding Values to Custom Taxonomies

There are multiple ways to add values to a custom taxonomy. One option is to visit the “Add New” or “Edit” page of any record in the post type to which you assigned the custom taxonomy. You will find the custom taxonomy listed as a submenu below the post type’s sub-menus. For instance, if you assigned “CTax1” to the “Posts” post type, you will find a “Ctax1” submenu listed below the “Posts” sub-menus. From there, you can create custom taxonomies.

Assigning Custom Fields to Custom Taxonomies

You can also assign a custom field group to your custom taxonomy. To do this, follow these steps:

  1. Go to ACF >> Field Groups and click on “Add New.”
  2. Provide a title for your field group.
  3. Add field types by selecting them from the drop-down menu. Enter the field label, field name, and default value.
  4. In the “Settings” section, choose “Taxonomy” >> “is equal to” >> “your_taxonomy” from the drop-down menus.
  5. Click “Save Changes.”

Now, the custom fields you created will be assigned to your custom taxonomy, enhancing the flexibility and functionality of your website.

Difference between Custom Fields and Taxonomies

In Advanced Custom Fields (ACF), both custom fields and taxonomies are used to extend the WordPress content management system by adding additional data fields to posts, pages, custom post types, or other content types.

Custom Fields:

  • In ACF, you can define custom fields using its interface or functions that serve different purposes and are structured differently: and then assign them to specific posts or content types.
  • Custom fields are individual pieces of data that can be attached to posts or other content types.
  • They allow you to define specific data fields such as text, numbers, dates, images, files, etc., which users can then fill in when creating or editing content.
  • Custom fields are typically used for adding additional information specific to each post or page, such as a product price, author bio, event date, etc.Taxonomies:
    • Taxonomies are a way of grouping and organizing content in WordPress.
    • They classify content into different categories, tags, or custom hierarchical structures.
    • Examples of taxonomies include categories (hierarchical), tags (non-hierarchical), and custom taxonomies (which you can define according to your needs).
    • Like custom fields, Taxonomies are not individual data fields; instead, they represent broader classifications or relationships among content items.
    • In ACF, you can create custom taxonomies and associate them with your custom post types, allowing you to organize and classify your content in a more structured way.

In summary, custom fields are used for adding specific data fields to individual posts or content items. However, taxonomies are used for grouping and organizing content into broader categories or classifications. Both custom fields and taxonomies can be powerful tools for extending the functionality and organization of your WordPress site, and they can often be used together to create rich and structured content experiences.

WP Ultimate CSV Importer Pro

WP Ultimate CSV Importer Pro

Get Ultimate CSV/XML Importer to import data on WordPress faster, quicker and safer.

How to filter posts by custom fields and custom taxonomies on archive pages?

To filter posts by custom fields and custom taxonomies on archive pages using Advanced Custom Fields (ACF) in WordPress, you’ll need to use custom queries to modify the main WordPress loop. Here’s a step-by-step guide to achieving this:

  1. Create Custom Fields and Taxonomies: First, ensure you have created your custom fields and taxonomies using Advanced Custom Fields. You can create custom fields for posts using ACF’s interface, and you can register custom taxonomies using code in your theme’s functions.php file.
  2. Modify the Main Query: In your theme’s functions.php file or a custom plugin, add a filter to modify the main WordPress query on the archive pages where you want to apply the filtering. You’ll use the pre_get_posts action hook for this purpose.
     

php

function custom_archive_query( $query ) {

    if ( $query->is_archive() && $query->is_main_query() ) {

        // Modify the query as per your requirements

        // Example: Filter posts by custom field ‘custom_field_name’

        // $query->set( ‘meta_key’, ‘custom_field_name’ );

        // $query->set( ‘meta_value’, ‘desired_value’ );

        // Example: Filter posts by custom taxonomy ‘custom_taxonomy_name’

        // $query->set( ‘tax_query’, array(

        //    array(

        //        ‘taxonomy’ => ‘custom_taxonomy_name’,

        //        ‘field’    => ‘slug’,

        //        ‘terms’    => ‘desired_term’,

        //    ),

        // ));

    }

}

add_action( ‘pre_get_posts’, ‘custom_archive_query’ );

  1. Uncomment and Customize: Uncomment and customize the lines within the custom_archive_query function to filter posts by your custom fields and taxonomies. Replace ‘custom_field_name’, ‘desired_value’, ‘custom_taxonomy_name’, and ‘desired_term’ with the appropriate values from your ACF setup.
  2. Save Changes and Test: Save your changes to functions.php or your custom plugin file, and then visit the archive pages where you’ve applied the filtering. Ensure that the posts are being filtered according to your custom fields and taxonomies.

By following these steps, you’ll be able to filter posts by custom fields and taxonomies on archive pages using ACF in WordPress. 

How to get an ACF taxonomy image?

Below are the steps to get an ACF (Advanced Custom Fields) taxonomy image:

  1. Create a Taxonomy Field:
  • First, you need to create a taxonomy field in your ACF field group. When creating the field, make sure you select the “Taxonomy” field type.
  1. Assign the Field to the Appropriate Taxonomy:
    • After creating the taxonomy field, assign it to the desired taxonomy (e.g., categories, tags, or a custom taxonomy you’ve created).
  2. Set Up the Field for Image Selection:
    • Within the field settings, ensure that you’ve set it up to allow image selection. You can do this by choosing the appropriate field type (e.g., Image or File) and configuring any other settings as needed.
  3. Retrieve the Image in Your Template:
    • Once your field is set up and content has been assigned, you can retrieve the image associated with the taxonomy term in your template files.
    • You can retrieve the taxonomy field value using ACF’s get_field() function. Pass the name of the field and the taxonomy term ID.

For example
php

$image_id = get_field(‘taxonomy_image’, ‘category_’ . get_queried_object_id());

$image_url = wp_get_attachment_image_url($image_id, ‘full’);

Display the Image:

  • Once you have the image URL, you can use it to display the image in your template using HTML or any other markup you prefer. For example,

php

if ($image_url) {

    echo ‘<img src=”‘ . esc_url($image_url) . ‘” alt=”Taxonomy Image”>’;

}

Replace ‘taxonomy_image’ with the actual name/key of your ACF taxonomy field and ‘category_’ with the appropriate prefix for your taxonomy (e.g., ‘category_’, ‘post_tag_’, or ‘your_custom_taxonomy_’).

By following these steps and using ACF functions like get_field(), you can easily retrieve and display the taxonomy image associated with your custom taxonomy terms in WordPress templates.

Frequently Asked Questions (FAQ)
 

  1. How do I get a custom taxonomy category name in WordPress?

To get the name of a custom taxonomy category in WordPress, you can use the get_queried_object() function combined with the ->name property. This is particularly useful when you are inside a taxonomy template (e.g., taxonomy.php) or when viewing a single post associated with a specific taxonomy term. Here’s how you can do it:

php

$term = get_queried_object();

if ($term) {

    $category_name = $term->name;

    echo $category_name;

}

Explanation:

  • get_queried_object() retrieves the currently queried object, which in the context of a taxonomy template or a single post associated with a taxonomy term, will be the taxonomy term object.
  • We then check if $term is not empty (meaning we’re on a taxonomy page or post associated with a term), and if so, we retrieve the name of the term using $term->name.
  • Finally, we echo or use $category_name as needed to display or use the name of the custom taxonomy category.

echo $term->name; // to show the name

echo $term->taxonomy; // to show the taxonomy

echo $term->slug; // to show taxonomy slug

You can place this code within your template file (taxonomy.php or single.php, for example) wherever you want to display the name of the custom taxonomy category. It will dynamically fetch the name based on the current context.

To display the data,
 

<?php $term = get_queried_object();  ?>  

<h1 class=”category-title”><?php echo $term->name; ?><span class=”taxonomy-label”><?php echo $term->taxonomy; ?> (<?php echo $term->count; ?> articles)</span></h1>

<p class=”category-description”><?php echo $term->description; ?></p>

2. What is the difference between the_field() and get_field()?

In the Advanced Custom Fields (ACF) plugin for WordPress, both the_field() and get_field() are used to retrieve the value of a custom field that you’ve created. However, they differ in how they handle the output:

the_field() directly outputs the field value with formatting, while get_field() retrieves the value as a variable for further processing. Choose the appropriate function based on whether you need to immediately output the field value or if you need to manipulate it first.

Some common troubleshooting steps and considerations 

  1. Field Group at Taxonomy Level:
    • Ensure that you’ve assigned the field group to the appropriate taxonomy. When creating or editing a field group, you need to select the relevant taxonomy in the “Location” settings.
    • Check that the field group is enabled and active. Sometimes, field groups may accidentally be disabled or restricted to certain post types, which could affect their visibility.
  2. Get Field Not Working with Taxonomies:
    • If get_field() is not working with taxonomies, double-check that you’re passing the correct taxonomy term ID or object ID to the function. Ensure that you’re using the correct taxonomy-specific functions to retrieve the ID.
    • Verify that the field key or field name is correct. Typos in the field key or name can prevent get_field() from retrieving the data.
  3. Taxonomy Field Not Saving Data:
    • If data is not being saved for taxonomy fields, check the field group settings. Ensure that the field is set up correctly with the appropriate field type and settings.
    • Verify that there are no conflicts with other plugins or themes. Sometimes, conflicting code or hooks from other plugins/themes can interfere with ACF’s functionality.
  4. What could do, if the ACF Field does Not Appear on Custom Taxonomy:
    • Ensure that the custom taxonomy is correctly registered in your theme or plugin code. The field group will only appear for taxonomies that are registered with WordPress.
    • Ensure that the ACF plugin is installed and activated on your site. Without the ACF plugin, the field groups won’t function properly.
  5. What to do, if  I Can’t Get a Custom Post Type by Taxonomy:
  • To retrieve custom post types based on taxonomy terms, you can use WP_Query or get_posts() with the appropriate taxonomy parameters. Make sure you’re using the correct taxonomy name and term IDs.
  • Double-check the taxonomy registration code for your custom post type. Ensure that the taxonomy is correctly associated with the custom post type.

Conclusion

Creating custom taxonomies with the ACF plugin allows you to organise and categorise your website’s content according to your specific needs. By following the steps outlined in this guide, you can create custom taxonomies, assign field groups, and import values, providing a structured and efficient content management system for your WordPress site. Additionally, if you want to import and export ACF Custom Taxonomy values, check out the CSV importer plugin here.