• Resolved joeyrobinson

    (@joeyrobinson)


    Hi I’ve been using the following code to exclude certain posts within a set category from the sitemap:

    add_filter('wpseo_sitemap_entry', 'exclude_post_in_categories', 10, 3);
    function exclude_post_in_categories($url, $type, $post ) {
      $categories = array('category-slug');
       if(has_category($categories, $post->ID)) {
    	  return false;
      }
      return $url;
    }

    However whilst it works, it results in each post sitemap (i.e. post-sitemap1.xml , post-sitemap2.xml) each having a random number of posts listed, and in some cases none – I wasn’t sure if this would cause issues with the search engines / was good practice?

    From what I gather Yoast calculates the total number of posts, works out how many post sitemaps are required, and then removes the excluded posts leaving several sitemaps virtually empty? Is there anyway for Yoast to remove all excluded posts before calculating how many sitemaps are required (so there are no empty/blank sitemaps created?)

    I’ve tried the wpseo_sitemap_entries_per_page filter to see if this resets the count, but this doesn’t solve the issue?

    Thanks, Joey

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Support Md Mazedul Islam Khan

    (@mazedulislamkhan)

    If you want to exclude posts or pages from the XML sitemap, we recommend you to follow the code example given here. It looks like you are using an entirely different filter to exclude the items from the XML sitemap.

    Thread Starter joeyrobinson

    (@joeyrobinson)

    Thank you for your reply Md Mazedul Islam Khan – is there a solution to this that will exclude posts based upon the category they are in? Mine works, it just causes the issue with the sitemap pagination?

    i.e. all posts in category XYZ get blocked from the sitemap.

    I’m sure it’s not practical or efficient coding to have an array of around 8000 post IDs to be blocked from the sitemap using the wpseo_exclude_from_sitemap_by_post_ids method on that page? Thanks, Joey

    • This reply was modified 4 years, 5 months ago by joeyrobinson.
    Sa?a

    (@stodorovic)

    The filter wpseo_sitemap_entry triggers for each entry and it isn’t perfect related to performance (hundreds calls of has_category). You can do same with the filter wpseo_exclude_from_sitemap_by_post_ids (via single “slow” query instead of hundredths queries). More examples – https://github.com/Yoast/wordpress-seo/issues/387

    However both filters remove entries from already “paginated sub-sitemaps” and it’s possible that some sub-sitemap will be empty (if you have massive exclusions). Empty sub-sitemap doesn’t make any issues related to search engines.

    There are couple possibilities to avoid your issue:

    • Use the filter wpseo_sitemap_entries_per_page to prevent pagination (set it to larger number than number of all posts). An issue could be size of XML file (max size is 50MB and 50000 URLs) and resources on the server (memory, CPU).
    • Use filters wpseo_posts_join and wpseo_posts_where (wpseo_posts_join and wpseo_typecount_where for sitemap index) to adapt SQL query before the pagination. You need extensive SQL/WordPress knowledge because you need to join table term_relationships and filter results by term_taxonomy_id.
    Thread Starter joeyrobinson

    (@joeyrobinson)

    Hi Sa?a

    Thank you for all of this info – I’ll have a look through it all – I’ve got a few options to go through, much appreciated.

    Another option might be to bulk set the value of “_yoast_wpseo_sitemap-include” to “never” for all the posts I wish to be excluded, and then add a function to functions.php to update this field whenever a post is added (change to “never”) or removed (change back to “always”) from the category in question?

    Thanks again

    Joey

    Sa?a

    (@stodorovic)

    I don’t recommend to you use post_meta fields because the query will be slower than with taxonomy. Also, it requires additional logic to handle this field. You can see my example on github, but it doesn’t solve your primary issue.

    The best solution is to use where filter. Something like this:

    {$wpdb->posts}.ID NOT IN (SELECT object_id
                                    FROM $wpdb->term_relationships
                                    WHERE term_taxonomy_id IN (11,12)
                            )
    

    It isn’t the code which you can just paste into functions.php, but I’ll try to make better snippet if I find little time tomorrow.

    Thread Starter joeyrobinson

    (@joeyrobinson)

    Thanks so much for this Sa?a, I really appreciate your help ??

    Sa?a

    (@stodorovic)

    I’ve done couple tests and it works fine with 7000-8000 products (it’s easier for me because there is good development environment). I’ll create more flexible solution in next couple days which will cover any post_type (post or product) or taxonomy (category or product-cat). I’ll probably publish it on github. Please be patient.

    Thread Starter joeyrobinson

    (@joeyrobinson)

    Thank you Sasa ??

    @joeyrobinson

    As the issue seems to be resolved we will go ahead and close this issue in order to keep the overview.

    If you experience any more issues or have questions please feel free to create a new topic!

    Thread Starter joeyrobinson

    (@joeyrobinson)

    Any chance you can keep it open for a few days in case Sasa has time to complete/publish a link to his/her solution?

    Sa?a

    (@stodorovic)

    @joeyrobinson I’ll update this thread probably tomorrow. Also you can follow github – Noindex all posts in category.

    Sa?a

    (@stodorovic)

    I just posted updated code on Noindex all posts in category.

    Thread Starter joeyrobinson

    (@joeyrobinson)

    Hi Sa?a – thank you so much for this, I really appreciate your hard work!

    I’ll test this out now, it looks ideal for my website (and hopefully other WordPress users will find it useful too!)

    Thanks again, Joey ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Removing posts from sitemap affects sitemap pagination?’ is closed to new replies.