disable product schema

#684111
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello, we apologize for the inconvenience you are facing. To disable product schema in products belonging to a specific category, you can use the following code snippet:

    /**
     * Filter to remove all Schema from certain product category pages
     */
    add_filter('rank_math/json_ld', function ($data, $jsonld) {
        if (is_product_category('category-slug')) {
            return [];
        }
        return $data;
    }, 99, 2);

    Replace ‘category-slug’ with the slug of the product category from which you want to remove the schema. You can also add multiple slugs if you want to remove schema from multiple categories by extending this code.

    If you have any further questions or need assistance with this, feel free to reach out to our support team here. We’re here to help!

    Sources:
    Remove All Schema from Product Category Pages » Rank Math
    Resolving Rank Math Schema Markup Related Issues

    monoor
    Rank Math free

    it did not work

    Hello,

    Thank you for contacting us and sorry for any inconvenience that might have been caused due to that.

    Please try to use the following filter on your website and see if that works for you:

    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function ($data) {
    	$slugs = array();
    	$product_cats = wp_get_post_terms(get_the_ID(), 'product_cat');
    	foreach ($product_cats as $product_cat) {
    		$slugs[] = $product_cat->slug;
    	}
    	
    	// Replace your_product_category with your product category slug.
    	$exclude_categories = array( 'your_product_category-1', 'your_product_category-2');
    
    	// Check if any of the exclude categories are in the product categories.
    	if (array_intersect($exclude_categories, $slugs)) {
    		return false;
    	}
    	return $data;
    } );
    

    Here’s how you can add filter/hook to your WordPress site: https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    monoor
    Rank Math free

    Thanks, it worked
    And that I rewrote this code by chatgpt, so that I can use the category id, the code works successfully, but I wanted to know if there is any problem with this code in your opinion?

    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function ($data) {
        $term_ids = array();
        $product_cats = wp_get_post_terms(get_the_ID(), 'product_cat');
        foreach ($product_cats as $product_cat) {
            $term_ids[] = $product_cat->term_id;
        }
        $exclude_category_ids = array( 1, 2);
        if (array_intersect($exclude_category_ids, $term_ids)) {
            return false;
        }
        return $data;
    } );

    Hello,

    If it works on your website, you can definitely use it without any issue.

    Hope that helps and please don’t hesitate to let us know if you have any other questions.
     
    Thank you.

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

You must be logged in to reply to this ticket.