Chúng tôi có thể giúp gì?
Categories
< Tất cả chủ đề

Thay đổi post_modified và post_date khi cập nhật (thu thập lại) bài viết

  • post_modified: Thời gian chỉnh sửa bài viết.
  • post_date: Thời gian đăng bài viết.

Để thay đổi post_modified của post khi cập nhật (thu thập lại) thì bạn phải sử dụng hook kdn/post/finished

Dán đoạn mã sau vào funtions.php của theme bạn đang dùng:

<?php
    // Action
    add_action('kdn/post/finished', 'kdn_post_finished', 10, 10);
    
    // Callback
    function kdn_post_finished($data, $postData, $postBot, $postSaver, $siteId, $postUrl, $urlTuple, $isRecrawl, $postId, $isFirstPage) {
        /**
         * Nếu đây là Thu thập lại và có post ID hợp lệ.
         */
        if ($isRecrawl && $postId) {
            $post = array(
                'ID' => wp_get_post_parent_id($postId) ?: $postId,
                'post_modified' => current_time('mysql'),
                'post_modified_gmt' => current_time('mysql'),
            );
            wp_update_post( $post );
        }
    }
?>

Như vậy bây giờ mỗi khi cập nhật bài viết thì post_modified  post_modified_gmt của chúng sẽ được cập nhật theo.

Tương tự vậy, nếu bạn muốn thay đổi post_date của bài viết khi Thu thập lại thì bạn sửa code trên như sau:

<?php
    // Action
    add_action('kdn/post/finished', 'kdn_post_finished', 10, 10);
    
    // Callback
    function kdn_post_finished($data, $postData, $postBot, $postSaver, $siteId, $postUrl, $urlTuple, $isRecrawl, $postId, $isFirstPage) {
        /**
         * Nếu đây là Thu thập lại và có post ID hợp lệ.
         */
        if ($isRecrawl && $postId) {
            $post = array(
                'ID' => wp_get_post_parent_id($postId) ?: $postId,
                'post_date' => current_time('mysql'),
                'post_date_gmt' => current_time('mysql'),
            );
            wp_update_post( $post );
        }
    }
?>

Tên thực tế bạn có thể thay đổi mọi thứ của bài viết bằng cách sử dụng các Parameters. Chúc bạn thành công!

Liên hệ