Support » Plugin: Yoast SEO » Snippet doesn’t exclude tag from sitemap

  • Resolved Wendihihihi

    (@wendihihihi)


    Hi,

    I’m trying to remove a tag from the sitemap.xml with this snippet in the functions.php

    /* Exclude One Taxonomy From Yoast SEO Sitemap */
    function sitemap_exclude_taxonomy( $value, $taxonomy ) {
    if ( $taxonomy == 'myslug' ) return true;
    }
    add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy', 10, 2 );

    This snippet didn’t remove the link on the post_tag-sitemap.xml page.

    My tag base permalink structure is domain.com/tag-base/tag-name. Could that be the reason?

    Thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • Saša

    (@stodorovic)

    The filter wpseo_sitemap_exclude_taxonomy excludes entire taxonomy (post_tag, category, product_cat, …) from root sitemap.

    It seems that you want to exclude particular term from the post_tag-sitemap.xml. You should use the filter wpseo_sitemap_entry .

    Plugin Support Jerlyn

    (@jerparx)

    Closed. No further questions.

    Yes sorry, I moved house this weekend. I haven’t had time to try it out yet but I think it will solve it.

    Thank you

    I still need some help. I’ve tried to replace wpseo_sitemap_exclude_taxonomy with wpseo_sitemap_entry but it will only delete every item in all sitemaps. I don’t know how to change this snippet. Can someone give an example?

    /* Exclude One Taxonomy From Yoast SEO Sitemap */
    function exclude_category_from_category_sitemap( $value, $taxonomy ) {
    if ( $taxonomy == 'mycategoryslug' ) return true;
    }
    add_filter( 'wpseo_sitemap_entry', 'exclude_category_from_category_sitemap', 10, 2 );
    Saša

    (@stodorovic)

    You can’t only replace filter name. The filter wpseo_sitemap_entry requires 3 arguments to you can check term slug (and it should return correct URL or false). Please don’t send wrong snippets, I’ll try to create correct code and I’ll post it here soon (today or tomorrow).
    Do you want to exclude particular term from the post_tag taxonomy?

    Hi, thanks for your help. I wish that Yoast has an easier option to remove items from the sitemaps lists.

    My url structure looks like this:

    http://www.domain.com/category/category-name for categories

    and

    http://www.domain.com/tag/tag-name for tags

    Would be great if you could give an example of the script. The only info on this I could find is this site: http://hookr.io/filters/wpseo_sitemap_entry/

    Thanks

    Saša

    (@stodorovic)

    The filter wpseo_sitemap_entry has 3 arguments: first is URL (it’s array), second is type (‘post’, ‘term’ or ‘user’) and third is the object. Example of usage:

    add_filter( 'wpseo_sitemap_entry', function( $url, $type, $term ) {
            // Return if it isn't taxonomy.
            if ( $type !== 'term' || ! is_object( $term ) ) {
                    return $url;
            }
    
            if ( $term->taxonomy === 'category' && $term->slug === 'my_slug' ) {
                    $url = array();
            }
    
            return $url;
    }, 10, 3 );
    

    Taxonomies are ‘category’, ‘post_tag’, … (don’t mix it with category_base or tag_base). More details about the term object – https://developer.wordpress.org/reference/classes/wp_term/.

    Please note that if you only exclude terms from sitemaps then they will be still indexed (archive pages don’t contain noindex robots tag). Googlebot will continue to index these pages. You should use the filter wpseo_robots in combination with previous code to properly exclude these pages from indexing.

    Thank you for the explanation Saša, that made it clearer. I’m going to try it later.

    Thanks

    One more question. How about multiple slugs? Not sure how to do this…

    Saša

    (@stodorovic)

    The most efficient way is in_array. Example for the conditional:

    if ( $term->taxonomy === 'category' && in_array( $term->slug, [ 'myslug1', 'myslug2' ] ) )

    Note: It doesn’t work on older PHP versions because I use [ instead of array. Anyway Yoast will drop support for PHP < 5.6 in next release.

    Perfect, thank you.

    I believe I’m on 7.3 so that wouldn’t give any problems.

    Thank you so much for all your help. It made it much clearer.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Snippet doesn’t exclude tag from sitemap’ is closed to new replies.