ifacethoughts

WordPress Hack – Category And Authors Without Posts

WordPress, by default, redirects you to the error page when you try to visit a category or an author without posts. This is acceptable in a lot of cases. However, in some cases, you might want to display them anyhow. Maybe you can show just the author profile with a message that the author has not authored any posts yet. I have not been able to find a non-hack solution to this yet, so here is the hack.

The 404 or the error page is issued by the method handle_404() of class WP in wp-includes/classes.php file. If certain conditions are met, it does not redirect. All we need to do is add our conditions to that. We can do this by adding !is_category() for category and !is_author for author. So the entire if statement looks like:

if ( (0 == count($wp_query->posts)) && 
      !is_category() && !is_author() &&
      !is_404() && !is_search() && 
      ( $this->did_permalink || (!empty($_SERVER['QUERY_STRING']) && 
      ( false === strpos($_SERVER['REQUEST_URI'], '?'))) ) 
   )

Now your template files for category and author will be executed even if they do not have any posts.

This is a hack, which I am not very fond of. I have used it in the Complete Wellbeing web site to handle profiles of multiple authors. But I will be looking for a better solution. I wonder if the redirection to the error page can be given to the individual templates. Also if more hooks can be added into WordPress so that certain parts of the template hierarchy are modifiable through plugins.

Discussion [Participate or Link]

  1. Lorelle said:

    This is an issue that has been a problem for me on my genealogy blog. Thanks for finding the answer.

    I also had to tackle the “similar” issue of Genealogy Blog: Blog Contributors – Wanted Dead or Alive. Creating a user for a dead person is more difficult than it first appeared. ;-)

  2. Abhijit Nadgouda said:

    :-)

Say your thought!

If you want to use HTML you can use these tags: <a>, <em>, <strong>, <abbr>, <code>, <blockquote>. Closing the tags will be appreciated as this site uses valid XHTML.

freshthoughts

contactme

Abhijit Nadgouda
iface Consulting
India
+91 9819820312
My bookmarks

badgesand...

This is the weblog of Abhijit Nadgouda where he writes down his thoughts on software development and related topics. You are invited to subscribe to the feed to stay updated or check out more subscription options. Or you can choose to browse by one of the topics.