WordPress.org

Forums

WP Query exclude category_name (3 posts)

  1. billbennett
    Member
    Posted 4 years ago #

    I've got two loops.

    One loop pulls the last four items from a category called "featured" thus:

    $my_query = new WP_Query('category_name=featured&showposts=4');

    I'd like to have a second loop which pulls the last 8 items for every category EXCEPT featured. I wrote it using the minus sign to remove the unwanted category as:

    $my_query = new WP_Query('category_name=-featured&showposts=8');

    But that does't work. Any ideas of a better way of doing this?

  2. Michael
    Forum Moderator
    Posted 4 years ago #

    turn the category name into a category id first:
    http://codex.wordpress.org/Function_Reference/get_cat_ID

    $cat_id = get_cat_ID('featured');

    then use:

    $my_query = new WP_Query('cat=-'.$cat_id.'&showposts=8');

    http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

  3. billbennett
    Member
    Posted 4 years ago #

    Thanks. This works.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags