How to Bulk NoIndex/NoFollow Posts or Pages in WordPress (No Plugin).
Quick Summary of Contents
- 1 Pre-Requisites and Things to Have on Hand:
- 2 Create Folder and File for Bulk NoIndex or NoFollow Script.
- 3 Gathering WordPress Posts and Pages for NoIndex or NoFollow.
- 4 Verify and Update the NoIndex or NoFollow Status.
- 5 Tutorial Video for Bulk NoIndex or NoFollow of WordPress Posts and Pages.
- 6 Function(s) to Check the NoIndex or NoFollow Status.
Hey there! So, let me tell you a story.
Almost ten years ago, I started blogging with the goal of helping people with search marketing their businesses online. However, I found my niche in the digital marketing and domain buying/selling space, as well as in solving WordPress website issues.
Recently, I noticed that my website’s search ranking was being negatively impacted by the massive amount of daily domain auction posts being indexed by Google – nearly 70% of my site’s pages!
So, I set out to find ways to programmatically bulk noindex and/or nofollow WordPress posts or pages, specifically by category, without using a plugin.
I searched high and low for a WordPress plugin that could help me out, but I couldn’t find one by Yoast or anyone else. However, I did find a no-plugin solution to bulk noindex and nofollow WordPress posts and pages using no plugin but rather a quick PHP tutorial that is easily automated via a cron job.
Now, I know what you might be thinking – why don’t I just filter the posts or pages by category in the WordPress admin and set them to draft or move them to trash?
Well, many of these pages are visited by domain investors days, weeks, and even months after their original publish date, earning me a lot of affiliate income. So, that’s not a feasible solution.
And for those of you wondering why I didn’t just programmatically no-index them from the beginning – well, I didn’t know what I didn’t know back then. The daily domain auction posts started out as a manual process, but then I was able to automate it using a PHP script and cron job.
Fast forward to today, and I have tens of thousands of pages that need to be no indexed. But don’t worry, I’ve got you covered with the steps on how to bulk noindex or nofollow WordPress posts or pages using Yoast and without a plugin, specifically by category. Let’s get started!
Pre-Requisites and Things to Have on Hand:
Alright, folks, let’s make sure we have everything we need to follow along with this tutorial:
- A trusty text editor like Sublime or NotePad++. These will help us make edits to our code.
- A WordPress website that has the Yoast plugin installed.
- A web host account that supports PHP (which should be the default for WordPress websites).
- A web host account that lets you create cron jobs. Cron jobs are nifty little automation tools that can run our script at set times.
- Access to the root directory of your WordPress website. You can get this through cPanel’s File Manager or FTP/SFTP software like FileZilla.
Don’t worry, I’ll explain what all of these things are and why we need them as we go along!
Create Folder and File for Bulk NoIndex or NoFollow Script.
I recommend that you create a directory in the root folder of your WordPress website as one of the first steps.
To do this, navigate to the root or public_html directory and create a new directory called “scripts” using either FileZilla or your cPanel’s File Manager.
Next, create a PHP file named ‘bulk-noindex-nofollow.php’ inside the ‘scripts’ directory. If you don’t have access to File Manager, you can use Sublime or NotePad++ to create the file and then upload it to the scripts folder using FileZilla.
Gathering WordPress Posts and Pages for NoIndex or NoFollow.
So, in the first tutorial step of PHP scripting and programming, we need to require WordPress’ wp-load.php file, which allows access to WordPress functions, classes, and database authentication and access.
Next, we need to gather a list of posts or pages to noindex or nofollow. For this tutorial, I’ll be showing you how to noindex and nofollow posts based on two distinct WordPress categories. Although I chose categories for this tutorial, it’ll work for tags as well.
Now, let’s retrieve a list of posts by creating a $query variable and instantiating the WP_Query. Pass the class arguments for the category (cat – see category parameters documentation) of posts to be retrieved and the number of posts to return (posts_per_page – see pagination parameters documentation).
It’s important to set the posts_per_page variable to retrieve all posts or pages, so we don’t have to make multiple calls and times using the offset parameter as an additional argument passed to bulk noindex and nofollow instead of one call.
So here’s the deal: if you want to retrieve all of your posts or pages, make sure to set the posts_per_page parameter to -1. Trust me, it’ll save you time and effort in the long run. If you don’t set it to -1, you’ll end up having to make multiple calls and use the offset parameter to retrieve all the posts or pages you need. And let’s face it, who wants to do that? Plus, when you set posts_per_page to -1, you get all the results at once instead of having to paginate through them.
Now, if you’re dealing with a ton of posts or pages, it might be better to offset them to protect your server resources. This means that instead of updating all of your posts or pages at once, you can update them in batches, so your server doesn’t get overwhelmed. Just something to keep in mind!
Verify and Update the NoIndex or NoFollow Status.
Once we have a bulk list of posts, we can use an if statement to execute code only if we have bulk posts to verify and update. Next, we can create a while loop to iterate through the index of bulk posts. Within the while loop, we define and set the values for the following variables:
- $postID: holds the unique ID of each post returned by the WordPress function get_the_ID() with no argument passed.
- $isPostNoIndexed: holds a boolean value indicating whether or not a post is indexed using the robotNoIndexExists function and passing $postID as an argument.
- $notIndexedText: set to nothing and used later in the tutorial to display text to the web browser when executing the script.
After setting those variables, we can create an if statement to check if $isPostNoIndexed returns a boolean value of false. Within this if statement, we can set $notIndexedText to the value of ‘NOT’, indicating that the post is no longer indexed.
Next, we set the $robotData array key values using the yoast_indexable table column headings. These column headings will be the array key names of is_robots_index and is_robots_nofollow. To index or follow a post, we set the array key values to 0. To no index or no follow a post, we set the array key values to 1.
We also set the $postData array key values using the yoast_indexable table column headings of object_id, object_type, and object_sub_type. We set the value of object_id to $postID. The values of object_type and object_sub_type should be the same and should reflect whether we’re working with a post or a page.
Now that $robotData and $postData variable arrays are defined, we update the yoast_indexable table by passing the $yoastTable, $robotData, and $postData as arguments to the WordPress update query.
We place the WordPress update query with an if statement that produces a successfully updated $updateStatus variable or an update not needed or not performed $updateStatus variable if not updated.
We close out the if statements with the bracket or endif; closures, and the while loop with endwhile;. We also add an echo line to display a message to the web browser when executing the script. That’s it!
Tutorial Video for Bulk NoIndex or NoFollow of WordPress Posts and Pages.
Actually, there are a few more steps involved in this process. One important step is to test the script before running it on multiple posts or pages. To do this, you can uncomment the die() function below the echo statement, which will allow you to test the script on a single post or page. Once you have tested the script and verified that it is working correctly, you can comment out the die() function so that it can run in bulk to no index or no follow blog posts or pages as desired.
Another added benefit would be to automate the script by creating a cron job to run it daily or a few times a day, depending on how many posts or pages you have. Setting up a cron job can be done easily using one of the many available tutorials or services.
That’s all for now. Please let me know if you have any questions or encounter any technical challenges while working with this script.
Function(s) to Check the NoIndex or NoFollow Status.
The purpose of the robotNoIndexExists function is to determine whether a post or page has been marked as not indexed. This is done by checking whether the is_robots_noindex value for the post in the yoast_indexable table is set to the numeric value of one (1). If the post is marked as no index, the function returns a boolean value of true. Conversely, if it is not marked as no index, the function returns the boolean value of false.