# Use NewsArticle Schema for a Category in Rank Math

> Changes Article and BlogPosting entities in Rank Math's schema to NewsArticle on posts filed under the categories you choose.

- Type: PHP
- Runs: Frontend only
- Category: [Plugin Integrations](https://wpide.com/library/category/integrations/)
- Tags: rank-math, schema, news
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/rankmath-news-article-category/

## Code

```php
// Category slugs whose posts are news.
$wpide_rm_news_cats = array( 'news' );

add_filter( 'rank_math/json_ld', function ( $data ) use ( $wpide_rm_news_cats ) {
    if ( ! is_array( $data ) || ! is_singular( 'post' ) || ! has_category( $wpide_rm_news_cats, get_queried_object_id() ) ) {
        return $data;
    }

    foreach ( $data as $key => $entity ) {
        if ( is_array( $entity ) && isset( $entity['@type'] ) && in_array( $entity['@type'], array( 'Article', 'BlogPosting' ), true ) ) {
            $data[ $key ]['@type'] = 'NewsArticle';
        }
    }

    return $data;
}, 99 );
```

## Notes

Use category slugs. Other posts keep the type chosen in Rank Math.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Use NewsArticle Schema for a Category in Rank Math" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
