we are trying to optimize a heavy traffic WP site, and am looking for a way to get the data for these three WP functions with only one query, in order to reduce queries & load on DB server:


Is this possible?

EDIT-- adjusted above example code to correctly indicate that in this case, we're grabbing the title and permalink for a different post_id -vs- the one that's returned by get_the_ID();


These functions do not even call the database each time they are run. It just pulls them off the current post object that was returned by the database query that retrieved the posts. There's nothing to optimise here.

get_the_ID() and get_the_title() literally just return $post->ID and $post->post_title. get_permalink() is a little bit more complicated, because it needs to do logic to figure out what the URL should be, based on permalink settings etc., but it doesn't require additional database calls.

WordPress is not so poorly optimised that you need to do anything about these functions. If it's how the documentation tells you how to do it, and it's how the default themes work, then it's fine.

If you're concerned about the speed of your site, then install Query Monitor and at least check what queries are actually being made before deciding on a target.

  • Please see recent edit- my example code was missing that in this case, get_permalink() and get_the_title() are being used on a post_id that's different from the one that is returned by get_the_ID(). Thank you. – Richard Zack Jun 24 '19 at 15:33
  • 1
    Even still, the post will only be queried once and then the other functions will refer to that post. – Jacob Peattie Jun 24 '19 at 15:36
  • So the first get_the_title() call will gather all post details, and then subsequent calls like get_the_permalink() and get_comments_number() etc will already have all the data? Also, yea Query Monitor is super cool, but it doesn't work if we're working with an API system that's outside of Wordpress, just invoking it to get data (unless there's a way to do that?) Thanks again. – Richard Zack Jun 24 '19 at 15:53
  • 1
    That’s correct. – Jacob Peattie Jun 24 '19 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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