bba01
Forum Replies Created
-
Ok so I solved it by my self with the help of my dear friend ChatGPT.
I want to exclude all products with product category id 25, 96 and 97 from appearing in the site map. When using
add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {it doesn’t work because it’s too late in the sitemap generating process. This is the code that finally worked:
/**
* Rank Math: Exclude WooCommerce products with certain categories from product sitemaps.
*
* This version excludes the products at query level.
*
* Excluded product category IDs:
* 25 = example product category
* 96 = example product category
* 97 = example product category
*/
add_filter( 'rank_math/sitemap/enable_caching', '__return_false' );
/**
* Get excluded product category IDs, including child categories.
*/
function custom_rankmath_get_excluded_product_cat_ids() {
$parent_cat_ids = array( 25, 96, 97 );
$excluded_ids = $parent_cat_ids;
foreach ( $parent_cat_ids as $parent_cat_id ) {
$child_ids = get_term_children( $parent_cat_id, 'product_cat' );
if ( ! is_wp_error( $child_ids ) && ! empty( $child_ids ) ) {
$excluded_ids = array_merge( $excluded_ids, $child_ids );
}
}
$excluded_ids = array_unique( array_map( 'absint', $excluded_ids ) );
return $excluded_ids;
}
/**
* Add JOIN needed for excluding products by product_cat.
*/
function custom_rankmath_sitemap_exclude_product_cat_join( $join, $post_type ) {
if ( 'product' !== $post_type ) {
return $join;
}
global $wpdb;
// Avoid adding the same JOIN twice.
if ( false !== strpos( $join, 'custom_rm_tr' ) ) {
return $join;
}
$join .= " LEFT JOIN {$wpdb->term_relationships} AS custom_rm_tr
ON p.ID = custom_rm_tr.object_id
LEFT JOIN {$wpdb->term_taxonomy} AS custom_rm_tt
ON custom_rm_tr.term_taxonomy_id = custom_rm_tt.term_taxonomy_id ";
return $join;
}
/**
* Add WHERE clause to exclude products in selected product categories.
*/
function custom_rankmath_sitemap_exclude_product_cat_where( $where, $post_type ) {
if ( 'product' !== $post_type ) {
return $where;
}
global $wpdb;
$excluded_ids = custom_rankmath_get_excluded_product_cat_ids();
if ( empty( $excluded_ids ) ) {
return $where;
}
$excluded_ids_sql = implode( ',', array_map( 'absint', $excluded_ids ) );
$where .= " AND p.ID NOT IN (
SELECT tr.object_id
FROM {$wpdb->term_relationships} AS tr
INNER JOIN {$wpdb->term_taxonomy} AS tt
ON tr.term_taxonomy_id = tt.term_taxonomy_id
WHERE tt.taxonomy = 'product_cat'
AND tt.term_id IN ( {$excluded_ids_sql} )
) ";
return $where;
}
/**
* Apply to sitemap count query.
* This prevents Rank Math from creating empty product-sitemapXX.xml files.
*/
add_filter(
'rank_math/sitemap/post_count/join',
'custom_rankmath_sitemap_exclude_product_cat_join',
10,
2
);
add_filter(
'rank_math/sitemap/post_count/where',
'custom_rankmath_sitemap_exclude_product_cat_where',
10,
2
);
/**
* Apply to actual sitemap product query.
* This prevents matching products from being listed.
*/
add_filter(
'rank_math/sitemap/get_posts/join',
'custom_rankmath_sitemap_exclude_product_cat_join',
10,
2
);
add_filter(
'rank_math/sitemap/get_posts/where',
'custom_rankmath_sitemap_exclude_product_cat_where',
10,
2
);
/**
* Extra safety fallback:
* If any product still reaches the final sitemap entry stage, remove it.
*/
add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {
if ( empty( $object->ID ) || empty( $object->post_type ) || 'product' !== $object->post_type ) {
return $url;
}
$excluded_ids = custom_rankmath_get_excluded_product_cat_ids();
if ( has_term( $excluded_ids, 'product_cat', $object->ID ) ) {
return false;
}
return $url;
}, 10, 3 );Hi!
I have other published product categories. I’ve sent you links @ [email protected] and included the link to your latest answer in the mail so you know where to answer and continue the support. Thank you so much for helping me.
With best regards,
b
Hi again!
Do you have any more advice on how to solve this matter?
With best regards,
b
FYI2 – When running the second snippet I get a 404 when product-sitemap.xml
FYI
In RankMath -> Sitemap Settings -> Products I’ve enabled both Include in Sitemap and Include in HTML Sitemap.
Hi and thank you so much for trying to help me out with this matter!
- I’ve excluded the site maps according to my caching plugin.
- I’ve also flushed the RankMath cach according to instructions in the video.
- It didn’t work so I changed the second part of the code according to your instructions and did step number 2 again. Still doesn’t work.
What happens is that I have over 2000 (two thousand) site maps on my page.
Do you have any other ideas?
With best regards,
b
Hi @jeremrm and thanks for a swift reply!
When I run this code the products sitemap isn’t even created. I get a 404 when clicking the product site map link.
/b
Forum: Plugins
In reply to: [BjornTech Swish for WooCommerce] Förslag på funktionHej!
Jag skulle fundera på om du ska ha allting användaren fyller i på kassasidan. Shipping info och billing info. Sen vore det ju bra om det också vore utvecklarvänligt så man kan skapa sina egna variabler om man behöver.
/b
Forum: Plugins
In reply to: [VARGAL - Additional Variation Gallery for WooCommerce] Autoplay webm filesHi!
Oh yes there is, sorry didn’t see it because it’s white and my video was also very light so it drowns in the image. Is there a way of changing the icon color/image?Never mind, I fixed it with ChatGPT.
.woocommerce div.product .vargal-control-nav .vargal-thumb-video:before {
background: none !important;
-webkit-mask-image: url(‘/wp-content/plugins/vargal-additional-variation-gallery-for-woo/assets/images/play_button.svg’) !important;
mask-image: url(‘/wp-content/plugins/vargal-additional-variation-gallery-for-woo/assets/images/play_button.svg’) !important;
-webkit-mask-repeat: no-repeat; mask-repeat: no-repeat;
-webkit-mask-position: center; mask-position: center;
-webkit-mask-size: cover; mask-size: cover;
background-color: #000 !important; /* solid black */
}- This reply was modified 9 months, 1 week ago by bba01.
Forum: Plugins
In reply to: [VARGAL - Additional Variation Gallery for WooCommerce] Autoplay webm filesHi!
Yes that is correct about the icon.
With best regards,bb
Forum: Plugins
In reply to: [WPC Smart Wishlist for WooCommerce] Missing translatable stringsHi @janilyn409!
Thank you for that information. I still think it would be a nice feature to be able to translate those 4 strings from inside the plugin so there is no need to use an additional plugin just to translate these 4 strings…
Thank you for an awesome plugin!With best regards,
bb
Also, is there a way to completely remove the related products functionality from the system? Not only the output on the frontend?
HI!
It’s done. How do I share it with you privately?With best regards,
bb
Forum: Plugins
In reply to: [WPC Smart Wishlist for WooCommerce] Missing translatable stringsOne more string: “There are no products on the Wishlist!”