Can’t exclude category post page for custom post type

#585448
  • Resolved Anonymous
    Rank Math free

    Hey there,

    I am using Oxygen, Metabox and RankMath on one of the sites I am building. Everything is working perfectly except one small exception. When I create a custom post type, it creates a page for the post category and everything within the category. For example, I have a custom post type for Treatments. Within the treatments, there is a list of treatments that are offered. But a webpage is also created for treatments as well. To address this, I am creating a 301 redirect, but I also need to remove it from the sitemap. Since this is not a page I created manually, I cannot find the post ID to exclude it in RankMath’s sitemap options.

    Any thoughts?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    Thanks for contacting us and sorry for the inconvenience caused.

    The page you are referring to is the archive page of the post type and you can modify and use the following filter to exclude it from the sitemap: https://rankmath.com/kb/filters-hooks-api-developer/#change-remove-archive-url

    Here is how to add a filter to your site: https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Hope that helps. Let us know if you need any other assistance.

    Anonymous
    Rank Math free

    Thanks for the reply. I followed the instructions and uploaded the filter PHP code using WPCodeBox (version 2.0 – Snippets alternative).

    The archive pages are still appearing in the sitemap according to the sitemap_index link in RankMath’s settings.

    This is the code I applied:
    <?php

    /**
    * Filter the URL Rank Math SEO uses in the XML sitemap for this post type archive.
    *
    * @param string $archive_url The URL of this archive
    * @param string $post_type The post type this archive is for.
    */
    add_filter( ‘rank_math/sitemap/post_type_archive_link’, function( $archive_url, $post_type ){
    return $archive_url;
    }, 10, 2 )
    ?>

    Any insights?

    Hello,

    Could you please share the sitemap URL so we can check?

    Meanwhile, you have mentioned that your CPT is called “treatments”. So in this case, you may need to modify the filter (assuming it is the post type you have created):

    /**
    * Filter the URL Rank Math SEO uses in the XML sitemap for this post type archive.
    *
    * @param string $archive_url The URL of this archive
    * @param string $post_type The post type this archive is for.
    */
    add_filter( 'rank_math/sitemap/post_type_archive_link', function( $archive_url, $post_type ){
    	if ($post_type == 'treatments'){
     		return false;
    }
    return $archive_url;
    }, 10, 2 );

    Once done, please follow these steps to flush your sitemap cache:

    1. Flush the Sitemap cache by following this video screencast:
    https://i.rankmath.com/pipRDp

    2. Exclude the Sitemap files of the Rank Math plugin in your caching plugin. The cache could be via a plugin or from the server. For plugins or Cloudflare, please follow this article:
    https://rankmath.com/kb/exclude-sitemaps-from-caching/

    And add this filter to your active theme’s functions.php file:

    add_filter( 'rank_math/sitemap/enable_caching', '__return_false');

    Let us know how this goes.

    Thank you.

    Anonymous
    Rank Math free

    This did the trick! Thank you for the help!

    Hello,
    
    You’re welcome.

    We are super happy that this issue is already resolved. If you have any other questions in the future, know that we are here to help you.
    
    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.
    
    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.
    
    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

The ticket ‘Can’t exclude category post page for custom post type’ is closed to new replies.