Make WordPress Core

Opened 5 years ago

Last modified 5 years ago

#43661 new enhancement

Un-neccessary call to get_post() in get_body_class() method

Reported by: patrelentlesstechnologycom's profile pat@… Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.4
Component: Posts, Post Types Keywords: has-patch 2nd-opinion needs-testing
Focuses: multisite, performance Cc:


I'm working with a multisite installation and selecting a Post from the primary site to use as the queried_object on another blog when rendering the front end.

When the body_class() method is called in my header template I've encountered a problem with wp-includes/post-template starting on line 639:

639: } elseif ( is_page() ) {

642: $page_id = $wp_query->get_queried_object_id();

644: $post = get_post($page_id);

The queried object id and the post has already been defined on line 598 and 599:

$post_id = $wp_query->get_queried_object_id();
$post = $wp_query->get_queried_object();

With the call to get_post() it's invoking WP_Post::get_instance() which can't return the Post I've already set as the queried object because it doesn't exist on the current blog.

I'm aware it's a rare use case and there may be other issues I'll encounter, but as far as I can tell the bit of code mentioned above is redundant and can be removed.


Attachments (1)

unneccessary-call-to-get_post.diff (694 bytes) - added by mattkeys 5 years ago.
removing unneeded call out to get_post when we already have the post object

Download all attachments as: .zip

Change History (3)

#1 @SergeyBiryukov
5 years ago

  • Component changed from Formatting to Posts, Post Types

#2 @mattkeys
5 years ago

  • Keywords has-patch 2nd-opinion needs-testing added

I do not see any reason that get_post() needs to be called here. Comparing what is returned from $wp_query->get_queried_object() and from get_post() they are the same. I think this should be safe to be removed, I've attached a diff.

In your particular use case you *may* run into a similar issue with the call to get_pages() that happens shortly after the code you mentioned, where you may not get the 'page-parent' class applied when it should. But that may or may not be an issue in your implementation.

5 years ago

removing unneeded call out to get_post when we already have the post object

Note: See TracTickets for help on using tickets.