Dynamically exclude items from Yoast SEO sitemap

If you are like me, you love the Yoast SEO plugin; one of the, if not the, most popular plugin for WordPress.

One of its features is to exclude pages from the sitemap. You can do this by going to the ‘Excluded posts’ tab in Yoast’s SEO settings, as explained here.

Yoast SEO excluded posts

Personally I like a more dynamic approach to the matter, I like to easily see which items are excluded, so I use some functions to make it more ‘fluid’.

Note: The plugin Advanced Custom Fields is necessary for this, but which website doesn’t run this plugin nowadays. Almost a must-have, like Yoast SEO.

If you don’t have ACF you can still use it, but then you need to use WordPress’ custom fields and change the get_field functions to get_post_meta.

The steps are:

  1. Create an option to ‘mark’ an item as excluded (with ACF)
  2. Create a function within a transient to retrieve these items
  3. Store our excluded items in ‘Excluded posts’ setting after a post is saved and the value changes

1. Create an option to ‘mark’ an item as excluded

Create a new group in ACF, as explained here.
Add a field with the Field Name “beee_exclude_sitemap”as explained here.
Show it on the post types which you want excluded.

2. Create a function to retrieve these items

Add this to your functions.php.

On line 9 we retrieve the stored transient.
If it returns false (line 11) we create it.
Lines 14-26 is the actual query to retrieve the posts which have our custom ‘exclude’ field.
Line 28 checks if there is at least 1 result.
If so an empty array is created on line 30.
We then loop through the array (lines 31-34) and add the post ID to a new array (line 33), which we created on line 30.
We then change the array into a comma separated string (line 36).
We store it in a transient (line 39).
And finally we return the value (the excluded items as a string) on line 43.

3. Store retrieved items in ‘Excluded posts’ setting after a post is saved

Also add this to functions.php.

First we retrieve our excluded items transient (line 7).
We then check if the post we just edited has our excluded value set (line 10).
If it is set, it needs to exists in our excluded items transient.
On line 12 we check if our post id exists in our transient.
If it already exists, we don’t have to do anything.
If it doesn’t exists, we delete the current transient, since it needs to be updated (line 17).

If the value is not set, it shouldn’t exist in our transient.
On line 21 we check if our post id exists in our transient.
If it exists, we delete the current transient, since it needs to be updated (line 24).
If it doesn’t exist we don’t have to do anything.

We then retrieve our transient function again. Since it now returns as false, it will be created again from scratch (line 30).

We then retrieve the values which are stored in Yoast’s SEO settings (line 33).

We compare our values against Yoast’s values (line 36).
If they match we don’t have to do anything.
If they don’t match we need to update Yoast’s settings with our values (line 40).

And there you have it. Yes, I know it can be written a lot shorter, but I wanted to optimize it as much as possible. The benefit of doing it this way is that the query to retrieve all excluded items is not run after each save action.