• Resolved Jellico

    (@catsfoto1se)


    Hi!

    I’m trying to find someway to exclude posts with with a specified category from the site XML file.

    I’ve got an option for frontend postsers to select if they want their post to be searchable or not via a chosen category.

    So is there anyway to stop all indexing if the post is in the category donotallowpostb (Slug)
    (Automated functions, can’t edit the posts manually to select “no index” )

    I’ve already configured the “internal” search function, so if a post is in the category donotallowpost it won’t be found on the site, and no links exists, (unless you know the page name), and that’s why I want to restrict the access, so they don’t end up on Google…

    Is this possible?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Sa?a

    (@stodorovic)

    Please read https://github.com/Yoast/wordpress-seo/issues/387 for more details. You need two PHP snippets (which should be synchronized):

    • The filter wpseo_robots which should set noindex for particular post.
    • The filter wpseo_exclude_from_sitemap_by_post_ids which remove these posts from the sitemaps.
    Thread Starter Jellico

    (@catsfoto1se)

    Ok, let me see if I got this..

    So

    add_filter( 'wpseo_robots', 'wpseo_robots' );
    /**
     * Filter: 'wpseo_robots' - Allows filtering of the meta robots output of Yoast SEO.
     *
     * @param string $robotsstr The meta robots directives to be echoed.
     * @return string
     */
    function wpseo_robots( $robotsstr ) {
    	if ( is_single() && in_category( 23 ) ) {
    		return 'noindex,follow';
    	}
    	return $robotsstr;
    }

    This one, but I change the return ‘noindex,follow’; to return ‘noindex,nofollow’;, and of course I change the category id?

    And add this (add both) to my functions.php:

    add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', function( $excluded_posts_ids ) {
    	$args = array(
    		'fields'         => 'ids',
    		'post_type'      => 'post',
    		'category__in'   => array( 11, 12 ),
    		'posts_per_page' => -1,
    	);
    
    	return array_merge( $excluded_posts_ids, get_posts( $args ) );
    } );

    And change that to the right categories..

    But, what does you mean with running them synchronized?

    Sa?a

    (@stodorovic)

    Yes, they are examples… I mean that cat IDs should be same in both PHP snippets to avoid strange results (submitted noindex URL or something like this). Additionally if yoy change categories then it’s possible that google uses old sitemap (which will contain noindex URLs). Probably you need to ping google to get new sitemaps or remove/submit again them.

    Thread Starter Jellico

    (@catsfoto1se)

    I see (i hope)..
    So by implanting these (with the right category id) would work then without any issues?

    I’m not worried about an old sitemap, since the site is new, and no posts exists with that category yet..

    .. But I maybe should implant the code tomorrow. 02.30 is little late to do it..

    So, go for it?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hide posts from a specific category from site XML list’ is closed to new replies.