4

I am using WP REST API to pull blog posts into another site. Everything works great, but now I need to save the results into transients to prevent querying the blog every time. I am new to transients and also new to WP REST api, so I am looking for the correct/best way to do this.

I have a function get_blog_posts_by_tags($tags) which uses the WP REST API to retrieve the posts. Once I have the results in a variable I set the transient. However, $tags might change from page to page where these posts are displayed. How do I go about checking if the new WP REST API query is the same/different from the one already stored in the transient?

function get_posts_by_tags($tags){
    if(false === ($result = get_transient('rest-posts'))) {
        $args = array(
            'filter[orderby]' => 'date',
            'filter[posts_per_page]' => 4,
            'filter[order]' => 'DESC',
            'filter[post_status]' => 'publish',
            'filter[tag]' => $tags,
        );
        $url = 'http://blog.myblog.com/wp-json/posts';
        $url = add_query_arg($args,$url);
        $response = wp_remote_get($url);
        //Check for error
        if (is_wp_error($response)) {
            return sprintf( 'The URL %1s could not be retrieved.', $url);
        }
        //get just the body
        $data = wp_remote_retrieve_body($response);
        //return if not an error
        if (!is_wp_error($data)){
          //decode and return
          $result = json_decode( $data );
          set_transient('rest-posts', $result,24 * HOUR_IN_SECONDS);
        }
    }
    return $result;
}

I thought about storing the WP API query into another transient and then use get_transient() every time and check if the transient content (the query wp api query) matches the new wp api query. But that seems a bit redundant and I am not sure it's the best approach.

1 Answer 1

3

According to code in OP, the only thing that changes the result is the $tags variable.

In this case, the easiste way to do the trick is make the $tags part of the transient name.

function get_posts_by_tags($tags){

    $transient = 'rest-posts-' . md5(serialize($tags));

    if(false === ($result = get_transient($transient))) {

          // the rest of yout function here ...

          if (!is_wp_error($data)){
             $result = json_decode( $data );

             set_transient($transient, $result, 24 * HOUR_IN_SECONDS);
          }   
    }

    return $result;
}

So for every $tags there is a transient.

If different pages result in same $tags they will query (and store) same transient and everything should work as expected.

3
  • In this case the only thing that changes is the tag, and that's a good solution. However, what if there were other parts of the filter changing? Should I just take the same approach and have one value for each possible filter?
    – gdaniel
    Commented Jul 10, 2015 at 20:39
  • 1
    You can create the $args variable changing it per your needs, then just do $transient = 'rest-posts-' . md5(serialize($args)); so same arguments, same transient. @gdaniel
    – gmazzap
    Commented Jul 10, 2015 at 20:46
  • I love the md5(serialize()) solution!
    – jonbon
    Commented Dec 10, 2020 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.