Support » Plugin: The SEO Framework » Noindex a page via php

  • Resolved krisgunnars

    (@krisgunnars)


    Hello,

    Is there a way to noindex a page using php?

    As in,

    if (condition == true) {
    tsf_make_page_noindex();
    }

    I have some pages that should be noindexed if certain conditions are true and doing it manually is not an option.

    Thanks,
    Kris

Viewing 7 replies - 1 through 7 (of 7 total)
  • I found a way to do this via functions.php.

    Now I am trying to remove the pages from the sitemap, as well.

    This is the code I have, but it’s not working — the pages are still in the sitemap:

    // Remove financials pages with limited data from sitemap
    	add_filter( 'the_seo_framework_sitemap_exclude_ids', function( $ids = array() ) {
    
    		$ids[] = -1;
    
    		if (get_post_type() == 'stocks' && !is_search()) {
    
    			$page_type = get_field('page_type');
    
    			if ($page_type == 'financialsincq' || $page_type == 'financialsbsq' || $page_type == 'financialscfq') {
    
    				$root_id = get_field('root_id');
    				$api_data_raw = get_field('fundamentals_quarterly', $root_id);
    				list($data) = json_decode($api_data_raw, true);
    				$count = count($data['datatable']['data']);
    
    				if ($count < 5) {
    					$ids[] = $root_id;
    				}
    
    			}
    
    			else if ($page_type == 'financials' || $page_type == 'financialsbsa' || $page_type == 'financialscfa') {
    			
    				$root_id = get_field('root_id');
    				$api_data_raw = get_field('fundamentals_annual', $root_id);
    				list($data) = json_decode($api_data_raw, true);
    				$count = count($data['datatable']['data']);
    
    				if ($count < 1) {
    					$ids[] = $root_id;
    				}
    			}
    		}
    
    		return $ids;
    	}, 10, 1 );
    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Kris!

    I’m glad you found a way to filter the noindex state. I’m considering adding a filter for the metadata because WordPress doesn’t provide an easy way internally. See https://core.trac.wordpress.org/ticket/43949. That will solve both issues you’re facing easily.

    Now, the the_seo_framework_sitemap_exclude_ids filter expects a list of post IDs to exclude. There is no query set up when the filter runs, so get_post_type() and get_field() won’t work without specifying the post. They’ll only test for the homepage otherwise.

    So, you must loop over all the posts, and return a list of IDs that meet your criteria. This will be heavy on your server, and it’s best to mimic our queries as you do this. In that, I think it’s also best for me to add an action that hands out all the IDs we use so that you can loop over them and filter out the posts.

    To summarize: What you’re trying to achieve with the sitemap currently isn’t feasible. I made notes for two changes in the behavior, so it’ll be much easier for you to adjust and maintain after I implement them.

    Hey Sybre,

    Thanks for the reply, I look forward to the updates.

    For now, I have made a custom function that loops through all the posts when I run it manually and gives me an array of IDs to exclude, then I put the IDs manually into the filter in functions.php each time.

    It would be better to have a fully automated way to do it, but this works for now.

    Kris

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Kris,

    From the next update (v4.0.5), you can use this filter:

    add_filter( 'the_seo_framework_post_meta', function( $meta, $post_id ) {
    
    	// Example. Do your post meta magic here. Mind the performance!
    	if ( 42 === $post_id ) {
    		// -1 = force index, 0 = default, 1 = force noindex.
    		$meta['_genesis_noindex'] = 1;
    	}
    
    	return $meta;
    }, 10, 2 );
    

    (View commit)

    With that, you can replace the heavy and complicated the_seo_framework_sitemap_exclude_ids filter, and assess the indexability automatically per included sitemap item.

    Mind that we’re going to deprecate _genesis_noindex in a (far) future update, but when that time comes, you’ll be notified via deprecation logs. We’ll ensure forward-compatibility by merging the legacy indexes.

    Cheers!

    So putting the post IDs there will cause them to both be noindexed and excluded from the sitemap?

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Kris!

    The post ID is forwarded via that filter. All you need to do is run your logic and adjust the indexing state. Then, indeed, the post can conditionally have the noindex state, and be excluded from the sitemap automatically.

    See: https://pastebin.com/4Reaz5R6

    Sweet, this is super useful. Thanks a lot.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.