Last active
February 15, 2024 21:28
-
-
Save mustafauysal/84111633061bf2b40e8e5d7220b71a23 to your computer and use it in GitHub Desktop.
Quick fix to purge product archives when product updated.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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