Skip to content

Instantly share code, notes, and snippets.

@mustafauysal
Last active February 15, 2024 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustafauysal/84111633061bf2b40e8e5d7220b71a23 to your computer and use it in GitHub Desktop.
Save mustafauysal/84111633061bf2b40e8e5d7220b71a23 to your computer and use it in GitHub Desktop.
Quick fix to purge product archives when product updated.
<?php
add_filter( 'powered_cache_advanced_cache_purge_urls', function ( $urls, $post_id ) {
if ( 'product' === get_post_type( $post_id ) ) {
$urls[] = get_permalink( $post_id );
$urls[] = get_permalink( wc_get_page_id( 'shop' ) );
$terms = wp_get_post_terms( $post_id, [ 'product_tag', 'product_cat' ] );
foreach ( $terms as $term ) {
$urls[] = get_term_link( $term );
// If the term has a parent, add the parent term link to the urls
if ( $term->parent > 0 ) {
$ancestors = get_ancestors( $term->term_id, $term->taxonomy );
foreach ( $ancestors as $ancestor ) {
$urls[] = get_term_link( $ancestor, $term->taxonomy );
}
}
}
}
return $urls;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment