Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created an author's page using:

 <?php
  if(isset($_GET['author_name'])) :
  $curauth = get_userdatabylogin($author_name);
  else :
  $curauth = get_userdata(intval($author));
  endif;
 ?>

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.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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.

share|improve this answer
    
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;); ?>
share|improve this answer
1  
You should use get_posts() for secondary Loops, see When should you use WP_Query vs query_posts() vs get_posts()? –  Rarst Dec 1 '10 at 6:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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