0

'Yoast SEO' plugin has a filter to exclude posts from the sitemap: wpseo_exclude_from_sitemap_by_post_ids

This is the adviced usage (from the 'Yoast SEO' official documentation):

add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', function () {
  return array( 311, 322 );
} );

I'm trying exactly as suggested but '404page' plugin overwrites my array with this line of code:

add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', function () {
            return array( $this->settings()->get( 'page_id' ) );
          } );

I've tried to change the plugin as follow but nothing changed, the other plugin overwites my array:

add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', function ($alreadyExcluded) {
              var_dump($alreadyExcluded);
              $error_page_id = $this->settings()->get('page_id');
              return array_push($alreadyExcluded, $error_page_id);
          } );
  • I managed with a workaround setting the priority of my addfilter to 10000. But why my attempt to modify the other plugin didn't solve the issue? – Revious Oct 5 at 12:20
1

Filters get queued and sorted by priority, then order of occurrence. Filters' default priority is 10, unless otherwise specified. The order of occurrence can be affected by the order plugins get fired (alphabetical, by plugin name).

So, when you started poking at this, both filters had equal priority. That would mean a rather arbitrary factor caused one filter to take precedence over the other.

Therefore, the only method for ensuring that any given filter is applied is to ensure that it has a higher priority than the filter you are trying to modify/replace. So, in your case, 10000 was higher than necessary, but represented the most appropriate approach.

Hope this answer helps!

  • Thanks, just another question adding the $alreadyExcluded parameter to the function called by the filter is the correct approach to avoid overwriting previous filter's work? – Revious Oct 10 at 11:03

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.