Exclude Categories or Tags from Instant Articles

Exclude Categories or Tags from Instant Articles

Version 1.5.0 or greater

Use the code below to prevent articles in a certain category getting converted. Just replace 'noinstant' with the SLUG of your category.

          /**
           * Use this function to stop posts syncing via ALL methods.
           *
           * @param  bool $should_convert Whether the post should be converted or not.
           * @param  WP_Post $post The WP Post being acted upon.
           * @return bool
           */
          function wpna_custom_should_convert( $should_convert, $post ) {
            // Don't convert the post if it is in a certain category.
            if ( has_category( array( 'noinstant' ), $post ) ) {
              return false;
            }
          
          
            return $should_convert;
          }
          add_filter( 'wpna_should_convert_post_ia', 'wpna_custom_should_convert', 10, 2 );
          

Before Version 1.5.0

You can use the code below to only convert posts to Instant Articles that have a certain category or tag. Simply add the code to your theme's functions.php file and change 'my-category-slug' to the slug of the category you want to exclude.

/**
  * Use this function to stop posts syncing via the API & Crawler ingestion.
  *
  * @param  string $value   on or off
  * @param  string $name    Name of the option 'fbia_sync_articles'
  * @param  string $default
  * @param  int    $post_id
  * @return string
  */
function wpna_custom_post_sync( $value, $name, $default, $post_id ) {


  // Don't convert the post if it is in a certain category.
  if ( has_category( array( 'my-category-slug' ), $post_id ) ) {
    $value = 'off';
  }


  // Return 'on' to sync the post.
  // Return 'off' not to sync the post.
  return $value;
}
add_filter( 'wpna_get_post_option_fbia_sync_articles', 'wpna_custom_post_sync', 10, 4 );
add_filter( 'wpna_get_option_fbia_crawler_ingestion', 'wpna_custom_post_sync', 10, 4 );


/**
  * Use this function to remove articles from the RSS Feed.
  *
  * @return WP_Query
  */
function wpna_custom_feed_query( $query ) {


  if ( function_exists( 'wpna_get_option' ) ) {
    // Requested option name, / value to default to.
    $feed_slug = wpna_get_option( 'fbia_feed_slug' );


    if ( $query->is_feed( $feed_slug ) && $query->is_main_query() ) {
      $query->set( 'category__not_in', array('my-category-slug' ) );
    }
  }


  return $query;
}
add_filter( 'pre_get_posts', 'wpna_custom_feed_query', 10, 1 );