Forum Replies Created

Viewing 1 replies (of 1 total)
  • I got my solution of the topic.
    Main idea is to filter out posts placed in subcategories. So we can use negative values in cat variable of the query_posts().

    My steps:

    1. Determinig list of subcategories for current category and merging them together
      $subcategories = get_categories(array('type' => 'post', 'child_of' => $_GET['cat']));
      
      foreach ($subcategories as $i => $value) {
          $excluded_cats .= ",-".$value->cat_ID;
      }

      You’ve got something like ,-10,-5,-3 Note leading comma.

    2. Generating cat variable
      if (!empty($excluded_cats)) {
         $MainLoopArgs = array('cat' => $_GET['cat'] . $excluded_cats);
      }
      else {
         $MainLoopArgs = array('cat' => $_GET['cat']);
      }

      I include current category and exclude subcategories in one cat. Result is something like 4,-10,-5,-3.

    3. Passing $MainLoopArgs into query_posts()
      query_posts( $MainLoopArgs );

    Sorry, maybe my example not so graceful, but it works! 🙂

    P.S. Yes, you have to make (m)any validation checks of variables for realy robust app.

Viewing 1 replies (of 1 total)