WordPress Trac: Ticket #14408: Get author information in author template file without having to query the first posthttps://core.trac.wordpress.org/ticket/14408
<p>
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.
</p>
<p>
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.
</p>
<p>
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.
</p>
<pre class="wiki">$author_id = get_query_var( 'author' );
$author_description = get_the_author_meta( 'description', $author_id );
</pre><p>
I attached a patch that addresses this issue.
</p>
<p>
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.
</p>
<p>
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.
</p>
en-usWordPress Trachttps://core.trac.wordpress.org/chrome/site/your_project_logo.pnghttps://core.trac.wordpress.org/ticket/14408
Trac 1.0.1garyc40Sat, 24 Jul 2010 13:52:07 GMTattachment sethttps://core.trac.wordpress.org/ticket/14408
https://core.trac.wordpress.org/ticket/14408
<ul>
<li><strong>attachment</strong>
set to <em>author-template-patch.diff</em>
</li>
</ul>
Ticketgaryc40Mon, 26 Jul 2010 10:12:57 GMTkeywords sethttps://core.trac.wordpress.org/ticket/14408#comment:1
https://core.trac.wordpress.org/ticket/14408#comment:1
<ul>
<li><strong>keywords</strong>
<em>has-patch</em> added
</li>
</ul>
TicketnacinSun, 16 Jan 2011 02:10:58 GMTtype, milestone changedhttps://core.trac.wordpress.org/ticket/14408#comment:2
https://core.trac.wordpress.org/ticket/14408#comment:2
<ul>
<li><strong>type</strong>
changed from <em>defect (bug)</em> to <em>enhancement</em>
</li>
<li><strong>milestone</strong>
changed from <em>Awaiting Review</em> to <em>Future Release</em>
</li>
</ul>
<p>
We can always just use the queried object.
</p>
Ticketgaryc40Sun, 16 Jan 2011 02:23:21 GMTkeywords, status changed; owner sethttps://core.trac.wordpress.org/ticket/14408#comment:3
https://core.trac.wordpress.org/ticket/14408#comment:3
<ul>
<li><strong>keywords</strong>
<em>needs-patch</em> added; <em>has-patch</em> removed
</li>
<li><strong>owner</strong>
set to <em>garyc40</em>
</li>
<li><strong>status</strong>
changed from <em>new</em> to <em>assigned</em>
</li>
</ul>
Ticketgaryc40Mon, 17 Jan 2011 02:25:34 GMTattachment sethttps://core.trac.wordpress.org/ticket/14408
https://core.trac.wordpress.org/ticket/14408
<ul>
<li><strong>attachment</strong>
set to <em>garyc40.14408.diff</em>
</li>
</ul>
<p>
set <tt>$authordata</tt> when <tt>is_author = true</tt>
</p>
Ticketgaryc40Mon, 17 Jan 2011 02:26:25 GMTkeywords changedhttps://core.trac.wordpress.org/ticket/14408#comment:4
https://core.trac.wordpress.org/ticket/14408#comment:4
<ul>
<li><strong>keywords</strong>
<em>has-patch</em> added; <em>needs-patch</em> removed
</li>
</ul>
TicketobenlandMon, 29 Jul 2013 18:16:18 GMTattachment sethttps://core.trac.wordpress.org/ticket/14408
https://core.trac.wordpress.org/ticket/14408
<ul>
<li><strong>attachment</strong>
set to <em>14408.1.diff</em>
</li>
</ul>
TicketobenlandMon, 29 Jul 2013 18:28:58 GMTmilestone changedhttps://core.trac.wordpress.org/ticket/14408#comment:5
https://core.trac.wordpress.org/ticket/14408#comment:5
<ul>
<li><strong>milestone</strong>
changed from <em>Future Release</em> to <em>3.7</em>
</li>
</ul>
<p>
Updated patch.
</p>
<p>
We still have to call <tt>the_post()</tt> in author archives, it would be great if we could stop doing that. Maybe in 3.7?
</p>
TicketnacinFri, 23 Aug 2013 14:04:49 GMThttps://core.trac.wordpress.org/ticket/14408#comment:6
https://core.trac.wordpress.org/ticket/14408#comment:6
<p>
Replying to <a class="closed" href="https://core.trac.wordpress.org/ticket/14408#comment:5" title="Comment 5 for Ticket #14408">obenland</a>:
</p>
<blockquote class="citation">
<p>
We still have to call <tt>the_post()</tt> in author archives, it would be great if we could stop doing that. Maybe in 3.7?
</p>
</blockquote>
<p>
Is that a separate patch/issue, or is that what is being fixed here?
</p>
TicketnacinFri, 23 Aug 2013 14:16:48 GMThttps://core.trac.wordpress.org/ticket/14408#comment:7
https://core.trac.wordpress.org/ticket/14408#comment:7
<p>
Replying to <a class="closed" href="https://core.trac.wordpress.org/ticket/14408#comment:6" title="Comment 6 for Ticket #14408">nacin</a>:
</p>
<blockquote class="citation">
<p>
Replying to <a class="closed" href="https://core.trac.wordpress.org/ticket/14408#comment:5" title="Comment 5 for Ticket #14408">obenland</a>:
</p>
<blockquote class="citation">
<p>
We still have to call <tt>the_post()</tt> in author archives, it would be great if we could stop doing that. Maybe in 3.7?
</p>
</blockquote>
<p>
Is that a separate patch/issue, or is that what is being fixed here?
</p>
</blockquote>
<p>
obenland clarified in IRC this is what is being fixed here.
</p>
<p>
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.
</p>
<p>
I think the proper location for this is in wp::register_globals(), which already sets up $posts, $post, etc.
</p>
TicketobenlandFri, 23 Aug 2013 14:41:47 GMTattachment sethttps://core.trac.wordpress.org/ticket/14408
https://core.trac.wordpress.org/ticket/14408
<ul>
<li><strong>attachment</strong>
set to <em>14408.2.diff</em>
</li>
</ul>
TicketobenlandFri, 23 Aug 2013 14:42:59 GMThttps://core.trac.wordpress.org/ticket/14408#comment:8
https://core.trac.wordpress.org/ticket/14408#comment:8
<p>
Thanks so much for your feedback on this! I updated the patch.
</p>
TicketnacinFri, 23 Aug 2013 17:34:15 GMThttps://core.trac.wordpress.org/ticket/14408#comment:9
https://core.trac.wordpress.org/ticket/14408#comment:9
<p>
<tt>$wp_query-&gt;is_main_query()</tt> isn't needed here as we're always dealing with the main query in the <tt>wp</tt> class. I do like moving to the methods, it is more obvious what is going on.
</p>
TicketobenlandTue, 27 Aug 2013 15:40:50 GMTattachment sethttps://core.trac.wordpress.org/ticket/14408
https://core.trac.wordpress.org/ticket/14408
<ul>
<li><strong>attachment</strong>
set to <em>14408.3.diff</em>
</li>
</ul>
TicketobenlandTue, 27 Aug 2013 15:41:23 GMThttps://core.trac.wordpress.org/ticket/14408#comment:10
https://core.trac.wordpress.org/ticket/14408#comment:10
<p>
Updated patch.
</p>
TicketnacinFri, 20 Sep 2013 21:07:43 GMTowner, status changedhttps://core.trac.wordpress.org/ticket/14408#comment:11
https://core.trac.wordpress.org/ticket/14408#comment:11
<ul>
<li><strong>owner</strong>
changed from <em>garyc40</em> to <em>nacin</em>
</li>
<li><strong>status</strong>
changed from <em>assigned</em> to <em>accepted</em>
</li>
</ul>
TicketnacinMon, 23 Sep 2013 17:21:50 GMTstatus changed; resolution sethttps://core.trac.wordpress.org/ticket/14408#comment:12
https://core.trac.wordpress.org/ticket/14408#comment:12
<ul>
<li><strong>status</strong>
changed from <em>accepted</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
</ul>
<p>
In <a class="changeset" href="https://core.trac.wordpress.org/changeset/25574" title="Set up author data for the author template immediately, rather than ...">25574</a>:
</p>
<div class="message"><p>
Set up author data for the author template immediately, rather than waiting for the first the_post() call.<br />
</p>
<p>
This removes the need to call the_post() and rewind_posts() in an author template to print information about the author.<br />
</p>
<p>
props obenland.<br />
fixes <a class="closed ticket" href="https://core.trac.wordpress.org/ticket/14408" title="enhancement: Get author information in author template file without having to query the ... (closed: fixed)">#14408</a>.<br />
</p>
</div>
TicketnacinMon, 23 Sep 2013 21:43:26 GMThttps://core.trac.wordpress.org/ticket/14408#comment:13
https://core.trac.wordpress.org/ticket/14408#comment:13
<p>
In <a class="changeset" href="https://core.trac.wordpress.org/changeset/25585" title="Avoid a notice. see #14408, [25574].
">25585</a>:
</p>
<div class="message"><p>
Avoid a notice. see <a class="closed ticket" href="https://core.trac.wordpress.org/ticket/14408" title="enhancement: Get author information in author template file without having to query the ... (closed: fixed)">#14408</a>, <a class="changeset" href="https://core.trac.wordpress.org/changeset/25574" title="Set up author data for the author template immediately, rather than ...">[25574]</a>.<br />
</p>
</div>
Ticket