Different Article type (schema) for posts that have the selected category set

#96325
  • Resolved m4rny
    Rank Math pro

    Hi, I’m looking for a way to set a different Article type (schema) for posts that have the selected category set.

    If the post is in category x, I would like it to be able to change the type of article, e.g. from News Article to Blog Post. Is there any way to do this? Any filter or hook?

    I would be grateful for pointing me in the right direction with this.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Ok, I found a solution. Maybe it will be useful to someone. If the post is in the selected category, change its type, e.g. from NewsArticle to BlogPosting. We assume that all entries have NewsArticle set and for the set categories we change this type to BlogPosting.

    /**
     * Filter to change the schema data.
     * Replacing article type for selected categories
     * @param array $entity Snippet Data
     * @return array
     */
    add_filter( "rank_math/snippet/rich_snippet_article_entity", function( $entity ) {
        if ( is_single() && in_category( array( 'category1', 'category2' ) )) {
            $entity['@type'] = 'BlogPosting';
        }
        return $entity;
    });
    Sajid Khan
    Rank Math business

    Hello,

    Thank you for contacting Rank Math.

    You marked this as resolved, do you still need our assistance?
    If not, please let us know if we can close this thread.

    Looking forward to helping you. Thank you.

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

You must be logged in to reply to this ticket.