Skip Navigation

[Resolved] Taxonomy without childs

This thread is resolved. Here is a description of the problem and solution.

Problem:

The user wants to create a taxonomy called "Artists" for a CPT "Record" with the following restrictions:

Limit the taxonomy to a single selection.
Ensure the taxonomy is "childless," meaning no parent-child hierarchy is allowed.

Solution:

Limiting the Taxonomy to a Single Selection:

Toolset does not natively allow restricting taxonomies to a single choice. However, you can achieve this using custom JavaScript and CSS to ensure only one checkbox can be selected in the admin panel.

function restrict_taxonomy_to_single_selection() {
    if (is_admin()) {
        ?>
        <style type="text/css">
            #newartist_parent {
                display: none; /* Hide parent selectors */
            }
        </style>
        <script type="text/javascript">
            jQuery(document).ready(function($) {
                $('input[name="tax_input[artist][]"]').on('change', function() {
                    if ($(this).is(':checked')) {
                        $('input[name="tax_input[artist][]"]').not(this).prop('checked', false);
                    }
                });
            });
        </script>
        <?php
    }
}
add_action('admin_footer', 'restrict_taxonomy_to_single_selection');

Creating a "Childless" Taxonomy:

In Toolset > Taxonomies, change the taxonomy type to a flat taxonomy (like tags) instead of hierarchical. This will prevent parent-child relationships in the taxonomy.

Alternative Using Toolset Relationships:

For a more robust solution, consider creating a custom post type "Artist" and use a one-to-many relationship with the "Record" post type. This ensures each record has only one artist and avoids hierarchy issues.

Relevant Documentation:

https://toolset.com/lesson-placement/lesson-placements-1824625-1727273/

https://toolset.com/lesson-placement/lesson-placements-1824625-1727251/

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 3 replies, has 1 voice.

Last updated by Christopher Amirian 1 month, 4 weeks ago.

Assisted by: Christopher Amirian.

Author
Posts
#2789616
Annotation on 2024-12-22 at 18-39-25.png

Hi,
We would like to create a taxonomy called "Artists" that contains the artist of a CPT "Record".
Since there should only be one "Artist" assigned to a Record, can we limit this category to a single choice? Additionally, it should be "childless," meaning there should be no possibility of assigning it to a parent taxonomy.

Is this possible directly with Toolset?

Thanks

#2789696

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support. You can change the category type in the Toolset > Taxonomies to avoid having a hierarchy option then you can use.

It will be like tags in WordPress's default, but you can not restrict the number of tags assigned to a post.

I suggest that you create a second custom post type called artist.

Then you can create a one-to-many relationship between the Artist's custom post type and the Record custom post type.

So a record can only have one artist, but an artist can have many records.

In the process of adding a new record, you will be able to assign an artist post type to it in the edit screen.

I suggest that you check the documentation below:

https://toolset.com/lesson-placement/lesson-placements-1824625-1727251/

https://toolset.com/lesson-placement/lesson-placements-1824625-1727273/

Thanks.

#2789855

I agree that Toolset relationships are a great tool. Unfortunately, they don't work with many page builders, as these builders only recognize CPTs and taxonomies in their queries.

This seems to work; even if the parent still exists, it doesn't show the dropdown.

function restrict_taxonomy_to_single_selection() {
if (is_admin()) { // Ensure the script only runs in the admin area
?>
<style type="text/css">
/* Hide parent selectors*/
#newartist_parent {
display: none;
}
</style>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Target the taxonomy checkboxes
$('input[name="tax_input[artist][]"]').on('change', function() {
if ($(this).is(':checked')) {
// Uncheck all other checkboxes except the one selected
$('input[name="tax_input[artist][]"]').not(this).prop('checked', false);
}
});
});
</script>
<?php
}
}
add_action('admin_footer', 'restrict_taxonomy_to_single_selection');

#2789951

Christopher Amirian
Supporter

Languages: English (English )

Hi,

I'm glad that you managed to find a solution with normal categories.

Thanks.