Make WordPress Core

Opened 15 years ago

Closed 11 years ago

Last modified 11 years ago

#14408 closed enhancement (fixed)

Get author information in author template file without having to query the first post

Reported by: garyc40's profile garyc40 Owned by: nacin's profile nacin
Milestone: 3.7 Priority: normal
Severity: normal Version: 3.0
Component: Themes Keywords: has-patch
Focuses: Cc:

Description

In the template file author.php, Twenty Ten queries the first post of the author to get the author's profile information. This introduces a bug when the author has no blog posts yet, then the global $authordata is not set, as a result, no author information is displayed. Also, having to rewind the query later in the template is counter-intuitive.

One may argue that there's no need to display author information if that author doesn't have any blog posts yet. But I disagree. Theme developers might want to list custom posts on the author template file as well. For some sites, the author page is served as a member profile page for subscribers. Therefore having to query the first post in order to get the requested author information is a flawed approach.

One better way to fetch author information in the author template is by getting the query var 'author' for the author ID. Then use get_author_meta() with the second parameter to get the desired information.

$author_id = get_query_var( 'author' );
$author_description = get_the_author_meta( 'description', $author_id );

I attached a patch that addresses this issue.

In this patch, get_author_meta() is also modified to handle the "display_name" field correctly by applying the filter "the_author" whenever this field is fetched.

Another approach, which I haven't tested yet, is to set the $authordata global variable whenever the author query var is set. If that's possible, then we no longer need to supply $author_id to get_the_author_meta(). If anyone is interested in testing this approach, go ahead and create a patch.

Attachments (5)

author-template-patch.diff (3.4 KB) - added by garyc40 15 years ago.
garyc40.14408.diff (1.7 KB) - added by garyc40 14 years ago.
set $authordata when is_author = true
14408.1.diff (595 bytes) - added by obenland 12 years ago.
14408.2.diff (654 bytes) - added by obenland 11 years ago.
14408.3.diff (1.6 KB) - added by obenland 11 years ago.

Download all attachments as: .zip

Change History (18)

#1 @garyc40
15 years ago

  • Keywords has-patch added

#2 @nacin
14 years ago

  • Milestone changed from Awaiting Review to Future Release
  • Type changed from defect (bug) to enhancement

We can always just use the queried object.

#3 @garyc40
14 years ago

  • Keywords needs-patch added; has-patch removed
  • Owner set to garyc40
  • Status changed from new to assigned

@garyc40
14 years ago

set $authordata when is_author = true

#4 @garyc40
14 years ago

  • Keywords has-patch added; needs-patch removed

@obenland
12 years ago

#5 follow-up: @obenland
12 years ago

  • Milestone changed from Future Release to 3.7

Updated patch.

We still have to call the_post() in author archives, it would be great if we could stop doing that. Maybe in 3.7?

#6 in reply to: ↑ 5 ; follow-up: @nacin
11 years ago

Replying to obenland:

We still have to call the_post() in author archives, it would be great if we could stop doing that. Maybe in 3.7?

Is that a separate patch/issue, or is that what is being fixed here?

#7 in reply to: ↑ 6 @nacin
11 years ago

Replying to nacin:

Replying to obenland:

We still have to call the_post() in author archives, it would be great if we could stop doing that. Maybe in 3.7?

Is that a separate patch/issue, or is that what is being fixed here?

obenland clarified in IRC this is what is being fixed here.

Looking at the patch, I think it is in the wrong place. We don't set up any kind of globals within the loop — it only occurs in setup_postdata(). Also, it must only occur for the main query, not for any subsequent queries. In this case, calling setup_postdata() in WP_Query (even just for the main query) could lead to some side effects, as well.

I think the proper location for this is in wp::register_globals(), which already sets up $posts, $post, etc.

@obenland
11 years ago

#8 @obenland
11 years ago

Thanks so much for your feedback on this! I updated the patch.

#9 @nacin
11 years ago

$wp_query->is_main_query() isn't needed here as we're always dealing with the main query in the wp class. I do like moving to the methods, it is more obvious what is going on.

@obenland
11 years ago

#10 @obenland
11 years ago

Updated patch.

#11 @nacin
11 years ago

  • Owner changed from garyc40 to nacin
  • Status changed from assigned to accepted

#12 @nacin
11 years ago

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

In 25574:

Set up author data for the author template immediately, rather than waiting for the first the_post() call.

This removes the need to call the_post() and rewind_posts() in an author template to print information about the author.

props obenland.
fixes #14408.

#13 @nacin
11 years ago

In 25585:

Avoid a notice. see #14408, [25574].

Note: See TracTickets for help on using tickets.