Including pages in author displays
If you go to
It will show all the posts by me – a couple hundred. Go to
and you’ll find nothing. But look at
and you’ll see “13 authors to this blog”. rboyd is one of them, as he’s written not posts, but pages, like this:
Is it possible for the /author/ listings to include pages?
There does not seem to be an easy way to do this, as of wordpress 2.6 anyway. I have not found a function to return “all pages by this author,” which I think is what kgagne and I need.
wp_list_pages() claims to accept an author parameter, but it outputs the results directly rather than returning them as an array to the calling code, which is what I need (so the calling code can determine if there are any pages by a given author, for example).
Pages are stored in the wp_posts table, just like posts are; they are distinguished by the post_type column, which contains ‘page’ for pages and ‘post’ for posts. So it shouldn’t be difficult to write the function necessary…
The fix for this turns out to be relatively easy; I’ve put it into a simple plug-in with about 3 lines of actual code. See URL below.
This solution is not necessarily plug-and-play, though, because it will hand The Loop an object containing both posts and pages, interspersed. This may not be what you want. You’ll almost certainly want to modify your authors template (author.php) to process posts and pages separately; e.g., you could have two iterations of the loop, with the first skipping pages and the second skipping posts. Hint: within the loop, $post->post_type will be either ‘post’ or ‘page’. Don’t forget to use rewind_posts() between the two loops.
Anyway, the challenge with showing pages for authors is not simply retrieving them, as I suggested in my earlier message here. By the time the author.php template runs, in 2.5 anyway, WordPress has already served a 404 header for author-page requests if no posts were found. This means it’s necessary to either disable the 404 header, or have WordPress retrieve posts and pages at once (for author requests anyway).
Other solutions around the web advocate adding a test (&& is_author()) to handle_404() in classes.php, so that author-page requests don’t send a 404 even if no posts are found. The problem with this is that requests for bogus author names will also pass — there’s no validation of authors at this point. Also, this sort of mod gets overwritten when you update WordPress. No fun.
Some digging revealed that the parse_request() function in classes.php runs external filters against the request terms, prior to querying the database. For author requests, the $this->query_vars object contains a single term, ‘author_name’. My plugin tests for this term; if the term is found, the plugin appends another value to the array, setting ‘post_type’ to ‘any’.
Setting that value here overrides a default value of ‘post’ that would otherwise be set downstream. This simple change causes WordPress to retrieve both posts and pages for author-page requests. The correct 404 behavior for nonsense author names is retained.
The plug-in is known to work for version 2.5.1 and not tested anywhere else.
- The topic ‘Including pages in author displays’ is closed to new replies.