Filter to remove Article schema from Posts in certain Categories only

#68242
  • Resolved Dominic E
    Rank Math free

    Hello,

    I’m trying to remove the ‘Article’ schema from specific posts in a certain category.

    Any ideas? Everything I’m trying isn’t working.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    Thank you for contacting Rank Math today.

    Have you tried manually changing schema of the posts to none? The following is a filter you can add to your functions.php.

    
    /**
     * Filter to remove Schema Data from Posts.
     * Replace $schema_type with schema name like article, review, etc.
     * @param bool  $value true/false Default false
     * @param array $parts Post Data
     * @param array $data  Schema Data
     * 
     * @return bool
     */
    add_filter( "rank_math/snippet/rich_snippet_{$schema_type}", function( $value, $parts, $data ) {
      if(get_category() == "your category array/list"){
     return false;
      }
    }, 10, 3 );
    

    Looking forward to helping you. Thank you.

    ​​​​​​

    D E
    Rank Math free

    Thank you for your reply – the above code returns an error:

    Fatal error: Uncaught ArgumentCountError: Too few arguments to function get_category(), 0 passed.

    I used the following code:

    add_filter( "rank_math/snippet/rich_snippet_article", function( $value, $parts, $data ) {
      if(get_category() == "alerts"){
     return false;
      }
    }, 10, 3 );
    Alberto
    Rank Math business

    Hello,

    I have created an improved version of the snippet, in this case it will check it is in a valid post with categories set and will check each one (their slugs) to see if they match the slug you set:

    add_filter( "rank_math/snippet/rich_snippet_article", function( $value, $parts, $data ) {
    
      $postCategories = get_the_category();
    
      if(!empty($postCategories){
        foreach( $postCategories as $postCategory ) {
        echo $category->term_id . ', ' . $category->slug . ', ' . $category->name . '<br />';
           if($postCategory->slug == "add here the category slug"){
              return false;
           }
         }
      }
    }, 10, 3 );

    Looking forward to help you.

    D E
    Rank Math free

    Thank you – I was able to get it working now. Here’s the code I used in case someone else comes across this question in the future:

    This will hide the ‘article’ schema for the parent category listed in the conditional.

    add_filter( "rank_math/snippet/rich_snippet_article", function( $value, $parts, $data ) {
      $postCategories = get_the_category();
      if(!empty($postCategories)) {
         if($postCategories[0]->slug != "PARENT CATEGORY SLUG HERE") {
        	 return false;
         }
      }
    }, 10, 3 );
    Alberto
    Rank Math business

    Hello,

    First at all, thank you for sharing the code, I am sure it will help a lot of people.

    Since you said the code is now working for you, do you want us to close the thread? We will be here for any questions or issues you might have in the future.

    Looking forward to help you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘Filter to remove Article schema from Posts in certain Categories only’ is closed to new replies.