Filter for modifying the “exclude empty terms”?

#282281
  • Resolved Jens Filipsson
    Rank Math pro

    Hello,
    I recently switched to RankMath from Yoast and so far I must say I love it! Such a great and easy to use plugin!

    I have one particular use case that I haven’t been able to solve though and I would like to ask for your advice on it.

    I would like to add my WooCommerce Product Tags to the sitemap and I would also like to exclude them from the sitemap if they are empty.

    But, I have a special product category on the site called “Hidden”. If I check this category, I have modified the WooCommerce loops to exclude the posts of that category so that they won’t be visible on archive pages.

    Problem is, the posts categorized as “Hidden” can still have product tags and if all products in a tag archive is in the hidden category, the archive will be perceived as empty in a visitors and Google’s eyes.

    So, I wanted to ask you if there is a filter that could be used to modify the exclude empty tags from the sitemap to check if:

    There are products attached to the tag AND at least one of them do not belong to the “Hidden category”.

    If the above statement is false, I would like to exclude the tag from the sitemap, otherwise include it.

    Hope you can help me with this!

    My best regards,
    Jens.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Nigel
    Rank Math business

    Hello,

    Thank you for contacting Rank Math for help with custom sitemap filters.

    Please check out our filters and hooks API for developers. There are several filters you could combine to achieve the results you want.

    You can add a condition to check if an item is part of the ‘Hidden’ category, then apply a filter to the sitemap item before it gets added to the sitemap.

    Next you could add a filter to remove empty terms.

    If there are any other hooks I left out you can check the rest of them in the same article.

    I hope that helps. If you have questions, do not hesitate to ask. We are here to help.

    Thank you for your answer! I actually want to keep the individual items in the sitemap, it’s the terms logic I need to modify.

    The remove empty terms filter you suggested me seems to apply an exclude: true/false on all items in the array at once, no?

    Or do you mean I could loop the array to apply my logic on each item, remove items I don’t want in the array and then pass back a new array with true/false?

    What I mean is, I want to keep the setting in Rank Math to exclude empty product tags, but apply a filter for one more statement to be considered when deciding if the term is empty or not.

    Nigel
    Rank Math business

    Hello,

    Thank you for the additional information, I’m not sure I understand the filter logic you want to apply but I’ll do my best.

    The following filter can be used to modify/remove a single item from a sitemap.
    https://rankmath.com/kb/filters-hooks-api-developer/#filter-sitemap-item

    
    /**
     * Filter URL entry before it gets added to the sitemap.
     *
     * @param array  $url  Array of URL parts.
     * @param string $type URL type. Can be user, post or term.
     * @param object $object Data object for the URL.
     */
    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
    	return $url;
    }, 10, 3 );
    

    The above filter runs when each link is added to the sitemap, so you will not need a loop. You can get the current post ID from $object->ID from there you can apply whatever logic you need.

    To remove an entry from the sitemap use return false

    Below is a simple example that removes the post with ID 47 from the sitemap.

    
    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
        if (47 == $object->ID) return false;
    	return $url;
    }, 10, 3 );
    

    Last, on the question of the empty terms filter, it checks if there are posts in a term archive, if none are found, a sitemap is not created for the term archive.

    I hope that clarifies things. If you have questions, ask away. We are here to help.

    Thanks, i think I haven’t been able to explain things good enough, sorry about that.

    What I would like to do:

    Empty tag archives should not be included in the sitemap, which is existing functionality in Rank Math out of the box.

    But I would also like to exclude term archives (in this case product tags) when all products in the archive is also categorized as “Hidden”.

    So even if there are products tagged with a particular tag, I also need to check that not all of them are also categorized with “Hidden”. If that’s the case, the tag should be considered empty.

    Let’s say I have three products with the tag red, but all of them also have the category “hidden”, then it would mean that the tag archive should be considered empty:

    Tag        Category
    Red        Hidden
    Red        Hidden
    Red        Hidden

    I still want the products of the “red” tag archive to be added to the sitemap, but I would like to exclude the “red” tag archive from the sitemap.

    Hope I have been able to clarify things!

    Nigel
    Rank Math business

    Hello,

    The results you want can be achieved with the filters I provided earlier.

    If you have any specific challenges with the filters, we are here to help.

    Ah, I missed the part where you said that “Filter URL entry before it gets added to the sitemap” could be a term. Yes, then I should be able to solve it. Thank you!

    Anas
    Rank Math business

    Hello,

    Let us know how it goes.

    Meanwhile, if you have any other questions, please feel free to ask.

    We are here to assist you.

    Well, to be honest I haven’t been able to figure it out.

    Let’s say I have a set of WooCommerce products, all which are tagged with the same term of the product_tag taxonomy. Let’s call the term “red”. All these products are also categorized as “Hidden”:

    Tag        Category
    Red        Hidden
    Red        Hidden
    Red        Hidden

    Products in the “Hidden” category removes the products from the archive pages, but I still want them visible in the sitemap under https://site.url/product-sitemap1.xml

    But, since all products in the product_tag archive of the term “red” are also in the “Hidden” category, the archive page will be empty.

    The products are hidden in the archives, but still visible if you visit the product’s single page directly. That’s why the products have to stay in the sitemap, but I still want the term archive url to be excluded.

    The filters provided above, as far as I understand it, gives me an individual term of a taxonomy back. What I can’t figure out is how I can check that this term is part of the product_tag taxonomy and then crosscheck if all products belonging to this term is also in the category “Hidden” or not?

    If all of them are hidden, exclude the term url from the taxonomy archive, else include it.

    I usually code in javascript, so I’m trying to figure out PHP as I go. Sorry for my lack of experience in the field 🙂

    Nigel
    Rank Math business

    Hello,

    A condition to check if tags are empty and post has category hidden should work

    if (!has_tag() && has_category('hidden'))

    Reference:
    has_tag() https://developer.wordpress.org/reference/functions/has_tag/
    has_category() https://developer.wordpress.org/reference/functions/has_category/

    I hope that helps.

    Could someone please read my question again, thoroughly? When reading the suggestions you have provided, unfortunately it seems you haven’t understood what I’m after. Sadly, I’m not sure how else to put it to make it come through…

    Hello,

    This would require a high level of customization that falls outside the scope of Rank Math support.

    Essentially to achieve this you would need to run through all the products tags on your website and for each one check if the products have the specific category that should hide the products and thus remove the tag after that.

    The steps are the following:

    1. Circle through all of the tags and see what products belong to each tag.

    2. Get that list of products and check for the presence of the Hidden category on them.

    3. In case the category is present on all of them, filter the current tag where the loop is in and remove it from the sitemap using the filters mentioned above.

    We also recommend having a look at the documentation about WooCommerce and its hooks and filters here: https://woocommerce.github.io/code-reference/hooks/hooks.html

    Hope this helps get you closer to the final solution.

    Don’t hesitate to get in touch if you have any other questions.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘Filter for modifying the “exclude empty terms”?’ is closed to new replies.