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.
Opens WPIDE on
Code
// 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.