php - Don't update modified post date when user add a product review or comment?

admin2025-03-19  0

I am using WooCommerce and I added the following code on content-single-product.phptemplate to show last update of product:

<p class="data-label">Updated</p>
<p class="info"> <?php echo get_the_modified_time( 'F j, Y' ); ?> </p>

On single product pages I have enable reviews tab and comment tab so customer can add comment on a product.

The problem is when user add new review or comment to product, the last update date for the

product is changed to the date of its comment.

I don't want to change the last update date of product when user add new comment on any product.

I am using WooCommerce and I added the following code on content-single-product.phptemplate to show last update of product:

<p class="data-label">Updated</p>
<p class="info"> <?php echo get_the_modified_time( 'F j, Y' ); ?> </p>

On single product pages I have enable reviews tab and comment tab so customer can add comment on a product.

The problem is when user add new review or comment to product, the last update date for the

product is changed to the date of its comment.

I don't want to change the last update date of product when user add new comment on any product.

Share Improve this question asked Jun 3, 2020 at 18:55 Fabregas CescFabregas Cesc 236 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

WooCommerce seems to update the 'last modified' value of a product on every change. If you want to take a look at the source code, go to woocommerce\includes\data-stores\class-wc-product-data-store-cpt.php and check the function update.

This solution seems to work, however it could be improved to make sure it doesn't mess up with anything else. Basically checks if the request has info of a comment and filters the query that modifies the post date to skip it. I didn't find any earlier hook than the query filter.

add_filter('query', function($query){
    if(isset($_POST['comment']) && strpos($query, 'post_modified')){
        return '';
    }
    return $query;
});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1742376703a210481.html

最新回复(0)