WordPress Queries

A little while ago I had to display author pages for posts of a certain custom type. Let’s call it “portfolio.” Now, I’m still learning, but my understanding is that author page templates require the following logic:

A little while ago I had to display author pages for posts of a certain custom type. Let’s call it “portfolio.” Now, I’m still learning, but my understanding is that author page templates require the following logic:

  1. Go through at least one iteration of The Loop (by calling the_post())
  2. Use the_author_meta() or get_the_author_meta() to get the author info you want and display it accordingly
  3. If you also want to display the author’s posts on this page, call rewind_posts() to (you guessed it) rewind The Loop, or build a whole new query

You don’t need to explicitly build a query at the start of the template. That’s automagically taken care of you by WordPress. However, if you want to get more specific, here’s how it’s done:

global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'portfolio' ) );
query_posts( $args );

The first line makes the query object (which is a really complex beast I’m just starting to wrap my head around) visible to the script. The second adds an extra argument to the query string. The third performs a new query. Pretty simple, right?

Mind you, for author pages the above code is only necessary if you’re doing step (3). Otherwise you’re good.