I created an author's page using:

  if(isset($_GET['author_name'])) :
  $curauth = get_userdatabylogin($author_name);
  else :
  $curauth = get_userdata(intval($author));

and then a standard loop, which displays all posts' titles published by that author. I'm trying to separate the display of the posts, according to the category they belong to (1, 2 or 3), so I tried using

<?php query_posts('cat=1'); ?>

but then all my blog posts are displayed, not just that author's. Something is certainly wrong.

I know I have to use a custom query when there is more than one loop per post, but given that just using a single standard query, with the "cat" filter isn't working, I'm a bit lost.


If you're using an author template there's absolutely no need to setup(set) the author query parameters, they'll be setup ready in the query object present on the author page..

You could additionally avoid the need to create numerous queries(one per category currently), by iterating over the query you have, extracting the categories, and storing post IDs associated with given categories inside an array. First iteration you use to build the array of IDs divided by category name/ID(whatever you like), then rewind the query, iterate over the posts, continuing(skipping) results that don't match the first category in your newly built array of category IDs(or names). Again you follow by rewinding the loop, then iterating over it again for each category you have in the new array, continuing(skipping) posts when they don't match the current category iteration.

It's not something that will really make much sense until you see it, and gets a little more complex if you categorize your posts into multiple categories(where the categories can intersect across posts), but it will work(without any additional queries) for the current page(it obviously won't sort the full result set, so whilst a given page will sort, that overall sort won't carry across the sum of all pages for the result set).

If you think the above method sounds useful and you're not fussed about the aforementioned problem i don't mind providing an example.

  • Thanks t31os. Your solutions is sound, specially performance-wise. I ended up keeping my own anyway, changing query_posts with get_posts, as I'm still learning my php, but I'm ok with the loop. – Sergio Majluf Dec 7 '10 at 12:58

Solved in a much much much simpler way

I have one loop per each category I need to display, and the query is built using:

<?php query_posts('cat=1&author='.$curauth->ID;); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.