Users may quickly find similar content thanks to WordPress tags, which also help keep your site organized. However, using too many tags can be counterproductive. It can affect your site’s performance, confuse visitors, and even impact your SEO. That’s why restricting the number of tags per post is crucial.
Regrettably, WordPress doesn’t include native tools for setting tag restrictions. Still, you can address this with a few simple actions. Whether you prefer a code snippet, a plugin, or a script, there’s a solution tailored to your needs. This guide will show you how to safely and easily limit the number of tags in WordPress posts.
Tags help users find related content easily and improve site structure and SEO. However, using too many can have the opposite effect.
Here are some reasons to limit tags:
Most experts recommend using no more than 5–10 tags per post.
functions.php
If you’re comfortable editing your theme’s files, try this method.
Go to your WordPress dashboard. From the left menu, click on “Appearance” and then “Theme File Editor.”
functions.php
FileFind and click on functions.php
. Scroll to the bottom of the file.
function limit_post_tags($post_ID) {
$post_tags = wp_get_post_tags($post_ID);
$max_tags = 5; // Set your tag limit here
if (count($post_tags) > $max_tags) {
$tags_to_keep = array_slice($post_tags, 0, $max_tags);
$tag_ids = wp_list_pluck($tags_to_keep, 'term_id');
wp_set_post_tags($post_ID, $tag_ids);
}
}
add_action('save_post', 'limit_post_tags');
This snippet checks the number of tags used in a post. If it’s more than 5, it removes the extra tags.
If you prefer not to edit theme files, create a simple plugin.
Use FTP or File Manager in your hosting panel. Navigate to wp-content/plugins/
.
Name it something like limit-tags.php
.
<?php
/*
Plugin Name: Limit Post Tags
Description: Limit the number of tags for WordPress posts.
Version: 1.0
*/
function limit_post_tags_plugin($post_ID) {
$max_tags = 5;
$post_tags = wp_get_post_tags($post_ID);
if (count($post_tags) > $max_tags) {
$tags_to_keep = array_slice($post_tags, 0, $max_tags);
$tag_ids = wp_list_pluck($tags_to_keep, 'term_id');
wp_set_post_tags($post_ID, $tag_ids);
}
}
add_action('save_post', 'limit_post_tags_plugin');
Go to your WordPress dashboard. Click on “Plugins,” find “Limit Post Tags,” and click “Activate.”
If you’re already using ACF, this method will work well.
Go to Plugins > Add New and search for Advanced Custom Fields. Install and activate it.
Go to Custom Fields > Add New.
Set the field type to “Taxonomy” and choose “Tags.” Under “Appearance,” set it to “Checkbox” or “Multi Select.”
Set location rules to show this field when editing posts.
Unfortunately, ACF doesn’t limit selection by default. However, you can use JavaScript or an additional plugin, such as ACF Toolkit, to enforce limits.
You can also add a script that warns users when they select too many tags.
functions.php
Add the following code to load your script:
function limit_tags_script() {
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
const tagInput = document.querySelector('#new-tag-post_tag');
const tagList = document.querySelector('#post_tag .tagchecklist');
function countTags() {
let tags = tagList.querySelectorAll('span');
if (tags.length >= 5) {
alert("Maximum 5 tags allowed.");
tagInput.disabled = true;
} else {
tagInput.disabled = false;
}
}
tagList.addEventListener('DOMSubtreeModified', countTags);
});
</script>
<?php
}
add_action('admin_footer', 'limit_tags_script');
This script monitors the tag input area. If tags reach 5, it disables the input.
While setting a maximum number of tags helps, consider these additional tips:
Limiting the number of tags in WordPress posts is essential for organizing content effectively. It enhances the reader’s experience, aids in SEO, and streamlines your website. With the right guidance, using a code snippet, plugin, or JavaScript fix is straightforward. Remember to use correct tags and review them regularly. Organizing your tags not only helps readers navigate your site more easily but also improves the overall performance of your blog. Take control of your tags today and keep your WordPress site neat and well-organized.