• Resolved oneruffryder

    (@oneruffryder)


    So I’ve have a few custom post types in combination with the plugin called Unlist posts. They are indexed but what i want is for individual which are not (using unlist posts checkbox) to not be indexed.

    I’ve also tried making an ACF field with checkbox, per your doc but the post still shows up.

    Ideas. Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter oneruffryder

    (@oneruffryder)

    To add if i use seo yoast snippet and change the field to acf field i’ve created, seems to work then. But idk is it the right way or way the default acf way doesnt work. Ideally still would want to target unlisted posts via unlist post plugin as no index sign.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Do you have any code snippets that you’ve already tried that I could check out?

    I’m curious if you’ve made use of the algolia_should_index_searchable_post hook yet, which will pass a boolean value for whether or not a given post is currently set to be indexed, as well as a WP_Post object variable.

    With that, you could grab whatever you want that you can get with a WP_Post object and conditionally return a different boolean value to the filter.

    For example, with a quick snippet example:

    function do_hide_this_post( $should_index, $post_obj ) {
    	// For this example, $should_index is coming in as true.
    
    	$is_checked = get_field( 'do_hide_this_post', $post_obj->ID );
    	if ( $is_checked ) {
    		// Since we have our ACF field checked for the post, we want to not index, 
    		$should_index = false;
    	}
    
    	return $should_index;
    }
    add_filter( 'algolia_should_index_searchable_post', 'do_hide_this_post' );
    
    Thread Starter oneruffryder

    (@oneruffryder)

    Hey, well i’ve tried to spin more than 20 different things without luck. Until I just used yoast snippet and put an acf field there. In the end i thought maybe its a conflict with something else, but then i created a fresh wp install new algolia app and only a few posts for testing. Still no luck. Originally i ve used the one from website

    function wds_algolia_exclude_post( $should_index, WP_Post $post ) {
     
      // If a page has been marked not searchable
      // by some other means, don't index the post.
      if ( false === $should_index ) {
         return false;
      }
    
      // ACF Field.
      // Check if a page is searchable.
      $excluded = get_field( 'exclude_from_search', $post->ID );
     
      // If not, don't index the post.
      if ( 1 === $excluded ) {
         return false;
      }
     
      // If all else fails, index the post.
      return true;
    }
    add_filter( 'algolia_should_index_searchable_post', 'wds_algolia_exclude_post', 10, 2 );
    add_filter( 'algolia_should_index_post', 'wds_algolia_exclude_post', 10, 2 );

    In one custom loop i am using this way to exclude unlisted post. First i set this
    $hidden_posts = get_option( ‘unlist_posts’, array() );
    then inside custom arg i just say ‘post__not_in’ => $hidden_posts, and it works. Those two things i see as a way to target it, but no luck, any ideas how to modify for an example your snippet to be able to work with this? Thanks

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Shouldn’t be needing to do anything in loops with these filters, Simply put each post that is being considered would be getting passed through the filter.

    Beyond that, I’d need to see what values are actually being returned with your get_field and whatnot. I’m curious if it’s returning an integer 1/0 or boolean keywords true or false since you’re using the strict comparison there with the ===. Maybe try changing to true / false or change to == and see if that maybe makes a difference.

    Thread Starter oneruffryder

    (@oneruffryder)

    Hi Michael. With your snippet if i put true or false works fine. To go little back, on your reply, “Simply put each post that is being considered”. This is not practical for me, it would mean authors need an admin access and keep adding id’s to the array.

    Last resort is to use ACF field, but i am looking to avoid confusion and double action. Unlist post and pages plugin works fine, has a checkbox but can’t figure how to use it inside filter, to not index checked posts. I wasnt talking about bringing the loop, just tried tell how it could be used, more description the better.

    Plugin offers a way kind of by using
    $hidden_posts = get_option( 'unlist_posts', array() );

    example of one usage

    // Hide unlisted posts from sitemap.xml in Yeost SEO
    // Add this to your theme's functions.php
    // See: https://kb.yoast.com/kb/sitemap-shows-excluded-posts-pages/
    add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', function () {
      $hidden_posts = get_option( 'unlist_posts', array() );
      return $hidden_posts;
    } );

    I’ve tried it ofc with changing filter name, tried all i could find, but no luck, that’s why i am here looking for a help, basically how i can add it to your plugin filter so post selected with it, are not indexed,

    Kind regards

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    This is not practical for me, it would mean authors need an admin access and keep adding id’s to the array.

    I don’t know what array you’re referring to here, as I didn’t create any.

    You’re not needing to maintain anything here, other than a checkbox saying “this post should be indexed” or not. The filters I mentioned already automatically get ran on each searchable post in your install, and it works on just one post at a time. With that one considered post at a time, the filter callbac can fetch whatever setting/option/post meta value you want to use to denote whether or not to index the post, and return true or false. True if it should get indexed, false if it should not.

    It could be some sort of option that you maybe maintain elsewhere, an ACF Field checkbox, some Yoast SEO setting that may already be getting used to exclude a post from sitemaps or searchability. Whatever you want.

    Just as long as you return a boolean value for the algolia_should_index_searchable_post and algolia_should_index_post filters, everything else should be automated.

    Thread Starter oneruffryder

    (@oneruffryder)

    Hi, I thought you’re were saying to go with excluded id’s (not practical part).
    In case someone stumbles this, I’ve made a solution, this is what I was looking for

    add_filter( 'algolia_should_index_searchable_post', function( $should_index, WP_Post $post ) {
    	if ( false === $should_index ) {
    		return false;
    	}
    	$exclude_ids = get_option("unlist_posts", array());
    	return !in_array($post->ID, $exclude_ids);
    }, 10, 2 );

    For some reason, even though I am not using autocomplete, it was the only way to make it work. Instantsearch reindexe didn’t work, only with autocomplete. So’ve changed algolia_should_index_searchable_post to algolia_should_index_post as well.
    It was a custom post type, not sure is it a bug, as I was expecting it to work with instantsearch reindex.

    Thanks for your time Michael.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome. Glad to hear it sounds like you reached a solution in the end.

    would this filter cause a post to be deindexed if it had already been indexed?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I believe it should, yes.

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

The topic ‘Do not index certain posts’ is closed to new replies.