۱ بهمن فلاحی
رویکرد حل مسئله
جامعه وردپرس (برنامه نویسی) ایجاد شده در ۲۸ آذر ۱۴۰۲

درود
در نمونه‌ای که جناب صالحی مسئله را طرح کردند بخش از راه‌حل در فایل index.php و در بدنه و بخش delete در function.php اعمال شد. آیا نمی‌توان برای حل مسئله پاسخ را به صورت یک فانکشن کامل طراحی و در function.php قرار داد؟؟ مثال:

/**
 * Get cached posts for a specific page.
 *
 * @param int $page Page number.
 * @return array|bool Cached posts on success, false on failure.
 */
function get_cached_posts($page) {
    $cache_key = 'paged_posts_' . $page . '_page';
    $cached_posts = get_transient($cache_key);
    if (false === $cached_posts) {
        // If the cached data doesn't exist, fetch the posts, cache them, and update the tracking option.
        $paged_posts = get_posts(array(
            'posts_per_page' => 4, // Adjust as needed
            'paged' => $page,
        ));
        if ($paged_posts) {
            set_transient($cache_key, $paged_posts, 12 * HOUR_IN_SECONDS); // Adjust the expiration time as needed
            update_option('paged_posts_tracking', true);
        }
        return $paged_posts;
    }
    return $cached_posts;
}
/**
 * Clear cached posts when a post is modified.
 *
 * @param int $post_id Post ID.
 */
function clear_cached_posts_on_post_update($post_id) {
    // Get the post's page number or other relevant information.
    $page_number = get_post_meta($post_id, 'page_number', true);
    if ($page_number) {
        $cache_key = 'paged_posts_' . $page_number . '_page';
        delete_transient($cache_key);
        update_option('paged_posts_tracking', true);
    }
}
// Hook the functions to appropriate actions.
add_action('save_post', 'clear_cached_posts_on_post_update');

سلام،

بله میتوانید بصورت مستقل بنویسید و کار توسعه و نگهداری را راحتتر میکند و درصورت نیاز در فایل‌های دیگر میتوان صدا زد.

محسن موحد ۲۹ آذر ۱۴۰۲، ۱۹:۳۰