Continuation of Exclude Custom Post Type with specific status from Sitemap #4093

#429673
  • Resolved G
    Rank Math business

    Hi,

    Some time passed until I heard from our theme developer. He sent me a code snippet which doesn’t work, so I went ahead and wanted to test whether having a double robots tag which basically both says noindex would be a temporary fix. I went ahead and tested it on one specific job which Google is giving me an error for: https://fiks.nl/vacature/skillz-talent-workforce-communicatie-marketing-stage-sittard-12290/

    #-----------------------------------------------------------------#
    # Rank Math: filter the robots meta data.
    # @param array $robots The meta robots directives.
    # Remove action in wp_head
    #-----------------------------------------------------------------#
    
    add_filter( 'rank_math/frontend/robots', function( $robots ) {
    	if(get_post_type() == 'job_listing' && get_post_meta( get_the_ID(), '_filled', true )  && get_the_ID() == '12852') {
    		$robots["index"] = 'noindex';
    		$robots["follow"] = 'follow';
    	}
    	return $robots;
    });

    As you can see in the html it says ‘noindex’ twice now, so that works. The odd thing, it stays in my sitemap. I flushed the sitemap, cleared my website cache and made sure my sitemap isn’t cached as explained by Reinelle.

    My sitemap can be found here: https://fiks.nl/job_listing-sitemap.xml

    What else can I do? Looking forward to your answers. Thanks so much!

Viewing 15 replies - 1 through 15 (of 17 total)
  • G
    Rank Math business

    Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    When you change the robots meta settings via the filter you shared the sitemap will not see that tag until the page loads and as such it sees the previous robots meta setting and includes it in the sitemap.

    ​​​​​​​To remove the page from the sitemap you need to include its ID in the option to exclude IDs from WordPress Dashboard > Rank Math > Sitemap Settings > General.

    Alternatively, you can edit the following filter to parse the URL before getting added to the sitemap and exclude it: https://rankmath.com/kb/filters-hooks-api-developer/#filter-sitemap-item

    Don’t hesitate to get in touch if you have any other questions.

    G
    Rank Math business

    Thanks for your quick response. I have over 300 job posts that are currently in my sitemap that shouldn’t be there because of this issue. Excluding them all one by one will thus not be an effective solution.

    In the alternative solution I only fix it for the future posts right?

    Is there no solution that tells Rank Math to look at all posts on my site again and then decide whether it should add it or not to the sitemap?

    Prabhat
    Rank Math agency

    Hello,

    You can use the alternate method to filter all the URLs, future or existing. Depending on how the data is stored by your theme, the following filter can be further modified and used:

    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
    	if(get_post_type() == 'job_listing' && 1 !== get_post_meta( get_the_ID(), '_filled', true )) {
    		return $url;
    	}
    }, 10, 3 );

    After applying the filter, you might need to follow this screencast to update the sitemap: https://i.rankmath.com/pipRDp

    Let us know how that goes.

    Thank you.

    G
    Rank Math business

    Thank you Prabhat!

    Although your code got rid of all jobs in my sitemap. I’m still new to PHP, so this filter will check whether a post being a job_listing and returns empty for posts which are filled? I don’t really get the empty but I also tried it without it. It all seems to exclude all job posts from my sitemap.

    I basically want to exclude all job posts which have meta data where _filled = true OR another option would be to just add a filter for anything that is noindex to be excluded from the sitemap?

    Could you please help me with that? Thanks a lot!

    Prabhat
    Rank Math agency

    Hello,

    Please try the below filter:

    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
    	if(get_post_type() == 'job_listing' && true !== get_post_meta( get_the_ID(), '_filled', true )) {
    		return $url;
    	}
    }, 10, 3 );

    Since I don’t have a similar setup like yours, I’m unable to test it. However, let me explain the process to you.

    Basically, an approach you can follow is to “return” only those URLs from the filter which are either missing the _filled status or are not yet filled. Depending upon how the _filled data is stored, you can modify the condition.

    Alternatively, you can share the filter with your theme developers and they should be able to modify it based on the data they store to give you the desired output.

    Let us know how that goes.

    Thank you.

    G
    Rank Math business

    The annoying thing here is our theme developer is hard to reach and does not properly help us…

    So this code works, all job posts now have noindex if their status _filled is 1.

    add_filter( 'rank_math/frontend/robots', function( $robots ) {
    	if(get_post_type() == 'job_listing' && get_post_meta( get_the_ID(), '_filled', true )  && get_the_ID() == '12852') {
    		$robots["index"] = 'noindex';
    		$robots["follow"] = 'follow';
    	}
    	return $robots;
    });

    So could you maybe help out in writing a filter that excludes all job posts which have ‘noindex’ ? I think for that you don’t need my setup right?

    Would be really helpful and I really appreciate the time you guys are putting in to help me figure this out. Great support!

    Prabhat
    Rank Math agency

    Hello,

    Could you confirm if you tried the filter I shared in my last reply? https://support.rankmath.com/ticket/continuation-of-exclude-custom-post-type-with-specific-status-from-sitemap-4093/?view=all#post-429823

    If not, please try it and see if it helps.

    If the issue persists, please edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    G
    Rank Math business

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    G
    Rank Math business

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    G
    Rank Math business

    I indeed tried your solution, but this empties all job listings from our sitemap. I only need the filled jobs to be removed from it.

    Thank you very much for the support!

    Hello,

    The wp-admin you shared seems to be throwing a 404 on our end. Can you please verify so we can further troubleshoot the issue?

    Looking forward to helping you on this one.

    Thank you.

    G
    Rank Math business

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    G
    Rank Math business

    Apologies, I forgot we changed that permalink for security reasons.

    Prabhat
    Rank Math agency

    Hello,

    I logged into your website’s admin area and modified the filter to get the desired output:

    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){	
        $filled = get_post_meta($object->ID, '_filled', true);
        if( $filled == 1 ) {
            return false;
        }
    	return $url;
    }, 10, 3 );

    I’ve added the filter in the Code Snippets plugin that was already installed on the website. The Snippet title is Filter Job Sitemap.

    Could you please check and confirm if it is working properly?

    Looking forward to hearing back from you.

    Thank you.

Viewing 15 replies - 1 through 15 (of 17 total)

You must be logged in to reply to this ticket.