• rahulattrey

    (@rahulattrey)


    How to apply the caching in a Query while having a time ..

    WP_REST_Attachments_Controller?to return media elements for showing in the story editor’s media library. Query time doesn’t seem particularly slow. With a large posts table and many media items, such queries are definitely possible.

Viewing 1 replies (of 1 total)
  • Rajnish Vijya

    (@rajnishvijya)

    function get_media_items() {
    $cached_data = get_transient(‘media_items_cache’);

    if (false === $cached_data) {
        $query_args = array(/* Your query arguments here */);
        $query_result = new WP_Query($query_args);
        $processed_data = process_query_result($query_result);
        set_transient('media_items_cache', $processed_data, HOUR_IN_SECONDS);
        return $processed_data;
    }
    
    return $cached_data;

    }

    function process_query_result($query_result) {
    // Your processing logic here
    return $processed_data;
    }

    Change the $query_args and caching duration HOUR_IN_SECONDS as needed.

Viewing 1 replies (of 1 total)
  • The topic ‘Optimize Slow Query’ is closed to new replies.