# Mark Posts in a Category as NewsArticle in Yoast

> Switches the Yoast SEO Article schema type to NewsArticle for posts in the categories you list, which suits news and press sections.

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

## Code

```php
// Category slugs whose posts are news articles.
$wpide_news_categories = array( 'news', 'press' );

add_filter( 'wpseo_schema_article_type', function ( $type, $indexable = null ) use ( $wpide_news_categories ) {
    $post_id = ( is_object( $indexable ) && isset( $indexable->object_type, $indexable->object_id ) && 'post' === $indexable->object_type ) ? (int) $indexable->object_id : ( is_singular() ? get_queried_object_id() : 0 );

    if ( $post_id && 'post' === get_post_type( $post_id ) && has_category( $wpide_news_categories, $post_id ) ) {
        return 'NewsArticle';
    }

    return $type;
}, 10, 2 );
```

## Notes

Use category slugs. Other posts keep the type set in Yoast's settings.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Mark Posts in a Category as NewsArticle in Yoast" 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/
