0

I have my categories set to sort alphabetically:

<?php if (is_category()) { 
     $posts = query_posts($query_string . '&orderby=title&order=asc'); 
  } 
?>

I want to exclude one category so that it sorts by date. I tried this but it made all the cats come up blank:

<?php if (is_category()) { 
    $posts = query_posts($query_string . '&orderby=title&order=asc&exclude=15'); 
   }
?>

Any ideas?

  • 2
    query_posts should not be used. you should be using WP_Query or pre_get_posts. In which template are you using this code – Pieter Goosen Jun 10 '14 at 11:23
  • ThemeMeme's Cassie – lkl Jun 10 '14 at 14:09
  • What I meant is is it index.php, category.php etc – Pieter Goosen Jun 10 '14 at 14:35
  • Sorry, in archive.php – lkl Jun 10 '14 at 14:43
0

Add an else to the condition along with adding the excluded category id in the is_category() call. You will also need to include/exclude that particular category depending on the condition that is true:

NOTE: you probably want to avoid using query_posts and use WP_Query() Reference here

 <?php 
    if (is_category(15)) {
        $posts = query_posts($query_string . '&orderby=date&order=asc&cat=15'); 
    }

   else {
     if(is_category()) { 
        $posts = query_posts($query_string . '&orderby=title&order=asc&cat=-15'); 
       } 
   }
 ?>
|improve this answer|||||
  • query_posts should not be used – Pieter Goosen Jun 14 '14 at 6:12
  • @PieterGoosen: he already had it in his code, I used it to clarify his own code as all he needed/wanted was the condition. – leromt Jun 14 '14 at 13:03
  • 1
    I just always think that someone must be made aware of something this wrong. I always make the OP aware of his problem and rectify it in my answer. The big thing here is, even the codex clearly states hat query_posts should not be used. – Pieter Goosen Jun 14 '14 at 13:11
  • @PieterGoosen: Pieter, I don't disagree but sometimes people just need help to solve a specific problem they have with functioning code. I think it's great that you point it out and you should continue to do so. As a matter of fact, based on all the times I've seen you comment about it, I've stopped using it in my own code. Thanks for that ! :-) – leromt Jun 14 '14 at 13:20

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.