Opened 6 years ago
Closed 13 months ago
#43661 closed enhancement (fixed)
Un-neccessary call to get_post() in get_body_class() method
Reported by: | Owned by: | 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)
Change History (7)
#3
@
15 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:
↓ 5
@
13 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
@
13 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!
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.