wordpress-seo icon indicating copy to clipboard operation
wordpress-seo copied to clipboard

Allow bulk excluding of Sitemap via URLs

Open ed6767 opened this issue 4 years ago • 6 comments

Is your feature request related to a problem? Please describe.

Our SEO partners often request us to remove URLs that contain specific strings, or other URLs that can't be filtered in the typical way with Yoast, or where filtering the posts manually would be unfeasible.

Currently there are a few ways to filter pages out of sitemaps, none of which are exactly feasible for this use case. The closest bodge is to modify the URL and deliberately cause duplication in the sitemap, which obviously we don't want but is better than a 404.

Describe the solution you'd like

Please add a filter function, similar to the ones here to filter a URL from the sitemap programmatically - for example, a function that takes in the URL, the returns a boolean whether or not it should be included in the sitemap.

Why do you think this feature is something we should consider for the Yoast SEO plugins?

It'd allow manual and custom programatic tweaking of site maps based on code. There is already a function to modify the URL, but not remove it from the blacklist.

Additional context

Yoast for Wordpress

ed6767 avatar Aug 15 '22 13:08 ed6767

There are already similar filters. The most efficient is wpseo_sitemap_entry - if the filter returns false then URL will be removed from a sitemap.

Related: https://wordpress.org/support/topic/exclude-list-of-urls-in-sitemap-xml/

stodorovic avatar Aug 16 '22 12:08 stodorovic

Thank you!! Would it be possible to update the documentation with this information? There doesn't seem to be any note of this in there as far as I'm aware.

ed6767 avatar Aug 16 '22 12:08 ed6767

I am wondering what's wrong with the wpseo_exclude_from_sitemap_by_post_ids that mentioned in the documentation already.

If you need more information about the filter wpseo_sitemap_entry, go and check out the relevant file where it was introduced here.

mmikhan avatar Aug 17 '22 21:08 mmikhan

When you have hundreds of posts that need excluding that filter isn't exactly practical

On Wed, 17 Aug 2022, 22:33 Md Mazedul Islam Khan, @.***> wrote:

I am wondering what's wrong with the wpseo_exclude_from_sitemap_by_post_ids that mentioned in the documentation https://developer.yoast.com/features/xml-sitemaps/api/#exclude-specific-posts already.

If you need more information about the filter wpseo_sitemap_entry, go and check out the relevant file where it was introduced here https://github.com/Yoast/wordpress-seo/blob/trunk/inc/sitemaps/class-post-type-sitemap-provider.php#L233 .

— Reply to this email directly, view it on GitHub https://github.com/Yoast/wordpress-seo/issues/18840#issuecomment-1218510748, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG4B6WFGS2ZDILUDUQYKCMDVZVLCXANCNFSM56SJG5AA . You are receiving this because you authored the thread.Message ID: @.***>

ed6767 avatar Aug 17 '22 22:08 ed6767

I can imagine. In that case, the other option is the one most likely you'll need to go for!

mmikhan avatar Aug 17 '22 22:08 mmikhan

Thank you!! Would it be possible to update the documentation with this information? There doesn't seem to be any note of this in there as far as I'm aware.

Agreed. I search for a long time before figuring it out. You could add it to this page ?

Anyway i give the code i added to my functions.php file to help other in need.

The code below remove all "child url" from a given url Exemple : URL you want to keep in the sitemap : https://yourwebsite.com/accueil-notice/

URL(s) you want to remove from the sitemap : https://yourwebsite.com/accueil-notice/loremipsum/loremipsum https://yourwebsite.com/accueil-notice/loremipsum2/loremipsum3 https://yourwebsite.com/accueil-notice/loremipsum

All URLs after "https://yourwebsite.com/accueil-notice/" are gonna be removed from your YoastSEO Sitemap.

/** Remove all child URL of https://yourwebsite.com/accueil-notice/ from YoastSEO sitemap **/
function filter_sitemap_entries($url, $type, $post) {
    // Vérifier si l'URL contient "/accueil-notice/" et n'est pas la page principale de "accueil-notice"
    if (strpos($url['loc'], '/accueil-notice/') !== false && $url['loc'] !== 'https://yourwebsite.com/accueil-notice/') {
        // Retourner un tableau vide pour exclure cet URL du sitemap
        return array();
    }
    
    // Sinon, retourner l'URL inchangé
    return $url;
}

// Ajouter le filtre
add_filter('wpseo_sitemap_entry', 'filter_sitemap_entries', 10, 3);

4Sitam4 avatar Jun 21 '23 14:06 4Sitam4