Support » Fixing WordPress » Exluding post categories from query_posts by name/slug with get_cat_ID

  • Resolved Shaun Brown


    Hi there, I have a custom loop for my blog page that Id like to allow “all” other categories aside from “events” to be visible in that particular feed…Im using the get_query_var($paged) trick to help maintain paging within query posts…anyways, since wordpress doesn’t afford you the luxury of directly excluding a category via name/slug instead of ID, it appears that using get_cat_ID is the solution, however I cant seem to amend my code properly in order for it to work.

    My goal is to simply show all posts with any category other then “events” basically….Heres my current code:
    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("category_name=blog&paged=$paged"); ?>

    As you can see, right now its only allowing posts with the category “blog” to show up, and thats fine, but ideally Id like to just exclude the “events” category and allow all others with get_cat_ID.

    Hope that was enough info and I explained it clear enough, thanks for the help.


Viewing 2 replies - 1 through 2 (of 2 total)
  • Michael


    Forum Moderator

    have you tried:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $cat_id = get_cat_ID('events');
     query_posts("category__not_in=$cat_id&paged=$paged"); ?>

    Man u guys rock at this forum every time I come through, thank you for the help, that worked perfect.

    It was this part that made it work: category__not_in=$cat_id cuz when I tried previously I was using 'cat=-' instead…

    What makes category__not_in=$cat_id unique compared to just 'cat=-' in this case?


    I appologize, i actually didnt use the exact same code you provided but it helped push me in the right direction, I changed it slightly to this and it worked:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $cat_id = get_cat_ID('events');
    query_posts("cat=-$cat_id&paged=$paged"); ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exluding post categories from query_posts by name/slug with get_cat_ID’ is closed to new replies.