1

I need to add multiple taxonomy filters to the functions.php file to remove (3) irrelevant taxonomies from the Yoast SEO sitemap index. I have been able to successfully add one filter, but when I add the other two filters, I keep getting a 500 server error. I should note that I am a novice when it comes to PHP so I imagine there is something fairly simple that I am missing here.

The filter that works by itself:

function sitemap_exclude_taxonomy( $value, $taxonomy ) {
if ( 'scope' == $taxonomy ) return true;
 }
 add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy', 10, 2 );

but the following code did not work for adding the two remaining filters:

function sitemap_exclude_taxonomy( $value, $taxonomy ) {
if ( 'scope' == $taxonomy ) return true;
 }
 add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy', 10, 2 );

function sitemap_exclude_taxonomy( $value, $taxonomy ) {
if ( 'layout_type' == $taxonomy ) return true;
 }
 add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy', 10, 2 );

function sitemap_exclude_taxonomy( $value, $taxonomy ) {
if ( 'module_width' == $taxonomy ) return true;
 }
 add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy', 10, 2 );

Many thanks in advance for the help!

-Mark

1

Try PHP's in_array.

function sitemap_exclude_taxonomy( $value, $taxonomy ) {
    $excludes = ['scope', 'layout_type', 'foo', 'bar'];
    if ( in_array( $taxonomy, $excludes ) return true;
}

add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy', 10, 2 );
1

You cannot have the same function name more than once. Instead, rename them:

function sitemap_exclude_taxonomy_1( $value, $taxonomy ) {
    if ( 'scope' == $taxonomy ) return true;
}
add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy_1', 10, 2 );

function sitemap_exclude_taxonomy_2( $value, $taxonomy ) {
    if ( 'layout_type' == $taxonomy ) return true;
}
add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy_2', 10, 2 );

function sitemap_exclude_taxonomy_3( $value, $taxonomy ) {
    if ( 'module_width' == $taxonomy ) return true;
}
add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy_3', 10, 2 );

Or better yet, combine them in one tidy function:

function sitemap_exclude_taxonomy( $value, $taxonomy ) {
    if (   'scope' == $taxonomy
        || 'layout_type' == $taxonomy
        || 'module_width' == $taxonomy )
       return true;
}
add_filter( 'wpseo_sitemap_exclude_taxonomy', 'sitemap_exclude_taxonomy', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.