When WordPress generates slugs for new posts, pages, or custom post type items, it automatically includes every word in the title. As a result, the slugs also incorporate "stop words" from the original title.
This can lead to long and unoptimized URLs, if the title contains words that add little value for search engines and visitors. Removing them can help create easier to read and more SEO-friendly URLs.
In this article, you will learn how to programmatically remove stop words from WordPress URLs using a simple code snippet, without any extra plugins.
Why You Should Consider Removing Stop Words from WordPress URLs
Stop words refer to common words like "a", "the", "in", or "of". When WordPress generates native slugs that are the core part of the canonical URLs, it includes these words by default, leading to unnecessarily lengthy permalinks.
For example, a post titled "The Complete Guide to Building a WordPress Blog" might generate this URL:
https://example.com/the-complete-guide-to-building-a-wordpress-blog
The red words are meaningless for search engine algorithms, so we can shorten them to make them readable.
https://example.com/complete-guide-building-wordpress-blog
SEO Impact
Shortening WordPress URLs by removing stop words is a good idea for several reasons. Search engines may not focus on URL keywords like they once did, but cleaner URLs still offer indirect SEO benefits.
By eliminating them from your WordPress permalinks, you can:
- Improve readability:
It is simpler for users to understand and remember URLs that are more concise. Additionally, they are less likely to get messed up when shared and simply look better when shared via social media. - Better CTR:
Keywords in URLs may not directly affect search engine rankings as much as they used to. Still, shorter URLs are simpler for users to read and remember, which could result in more clicks from SERP.
Should Stop Words Always Be Removed?
If you are still not sure whether removing stop words is a good idea, consider that Yoast SEO once offered this feature but removed it in March 2018.
They discovered it did not really help SEO and sometimes made URLs harder to understand. In some cases, keeping stop words and URLs intact is preferable for user experience. They are often necessary to keep the meaning and context of your URLs.
Here are some examples where removing them can change the meaning entirely:
https://example.com/where-to-eat-in-paris ==> https://example.com/where-eat-paris
https://example.com/the-art-of-war ==> https://example.com/art-war
https://example.com/the-sound-of-silence ==> https://example.com/sound-silence
Considering everything mentioned earlier, you should understand that removing stop words might not directly improve your SEO in the way you expected.
Every website is different, so the best strategy for yours will depend on your specific SEO goals and plans.
How to Remove Stop Words Automatically?
To remove stop-words from slugs in WordPress automatically, simply add the following code to your website's codebase. If necessary, feel free to extend and change it.
You can customize which words are removed, by editing the
$stop_words
array. For example, if you want to keep the word "and", just remove it from the list inside the code.
Custom Code Snippet
function pm_is_stop_word($word) {
	$stop_words = array(
		'a', 'an', 'and', 'are', 'as', 'at',
		'be', 'by', 'for', 'from', 'has',
		'in', 'is', 'it', 'of', 'on', 'that',
		'the', 'this', 'to', 'was', 'were',
		'will', 'with'
	);
	return in_array($word, $stop_words);
}
function pm_remove_stop_words_from_slug($slug) {
	// Split slug into words
	$words = explode('-', $slug);
	$filtered_words = array();
	foreach ($words as $word) {
		if (pm_is_stop_word($word)) {
			continue;
		}
		$filtered_words[] = $word;
	}
	// Handle empty slugs
	if (empty($filtered_words)) {
		return $slug;
	}
	// Rebuild and return the clean slug
	return implode('-', $filtered_words);
}
add_filter('wp_unique_post_slug', 'pm_remove_stop_words_from_slug');
This snippet runs automatically whenever WordPress generates a unique slug for posts, pages, or categories. The logic behind it is simple. By using the wp_unique_post_slug filter, it removes unwanted predefined words from the slugs.
How To Handle Existing URLs?
After the slug is generated, WordPress will not update automatically. Therefore, if you want to remove stop-words from the URLs of existing posts or terms, you will need to regenerate them manually.
The easiest way to recreate native slugs is by using the Permalink Manager’s "Regenerate/reset" tool. Before you begin, make sure to switch to “Native slugs” mode and do a SQL backup.
When removing stop words from existing URLs, make sure to set up fallback redirects from the old URLs to the new ones. The redirects will prevent "404 Not Found" errors and help you keep your SEO rankings by passing link equity.
Leave a Reply