Make WordPress Core

Opened 6 years ago

Closed 7 months ago

#43661 closed enhancement (fixed)

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

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

Description

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.

Please?

Attachments (1)

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

Download all attachments as: .zip

Change History (7)

#1 @SergeyBiryukov
6 years ago

  • Component changed from Formatting to Posts, Post Types

#2 @mattkeys
6 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.

@mattkeys
6 years ago

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

#3 @spacedmonkey
9 months ago

  • Milestone changed from Awaiting Review to 6.4
  • Owner set to spacedmonkey
  • Status changed from new to assigned

Looks good to me.

#4 follow-up: @oglekler
7 months ago

@spacedmonkey there are also several calls for $wp_query->get_queried_object() in the same function, can we remove them as well? And actually it can be:

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

#5 in reply to: ↑ 4 @SergeyBiryukov
7 months ago

  • Keywords 2nd-opinion removed

Replying to oglekler:

there are also several calls for $wp_query->get_queried_object() in the same function, can we remove them as well?

It looks like they are all in different conditional branches, so each of them is only executed once, depending on which conditional is true: is_author(), is_category(), etc. So I think those are fine to keep as is for now.

And actually it can be:

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

That makes sense to me, thanks!

#6 @SergeyBiryukov
7 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 56424:

Posts, Post Types: Remove redundant function calls in get_body_class().

As part of a previous change to add support for post type templates, the $wp_query->get_queried_object_id() method ended up being called twice, in both the is_singular() and is_page() conditional branches.

The get_post() function call was also unnecessary, as $wp_query->get_queried_object() is already called in the is_singular() branch above, which includes the is_page() branch too.

This commit removes the redundant calls. The first $wp_query->get_queried_object_id() call is removed as well, since the post ID is already available via $wp_query->get_queried_object().

Follow-up to [10485], [10877], [12877], [13032], [21597], [38951].

Props mattkeys, spacedmonkey, oglekler.
Fixes #43661.

Note: See TracTickets for help on using tickets.