Support » Fixing WordPress » Query with multiple category_name?

  • Hi!

    Currently i am using the standard stuff:
    <?php query_posts('category_name=Nyheter&showposts=4'); ?>

    The problem is that it seems like there is no way to add one more category by name?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi

    I just tested this and it doesn’t appear to support more than one category by name, even when names are placed in an array.

    If you must do it by name and not by cat ID (doing it by ID is built into WP and requires no extra work for multiple cats) I would approach it by building a string of cat ID’s doing successive lookups of names one by one, and then feeding the string of ID’s into the query_posts.

    $catIDs = get_cat_ID( $cat_name='Nyheter' );
    $catIDs .= ',' . get_cat_ID( $cat_name='SecondCat' );
    $catIDs .= ',' . get_cat_ID( $cat_name='ThirdCat' );

    You must use double quotes on the query_posts doing it this way.

    If there are a lot of cats to look up, it could be turned into a loop using the cat names in an array at the top.

    The above code didn’t work for me. It was needed to do a little fix.

    $catIDs = get_cat_ID( $cat_name='Nyheter' );
    $catIDs .= ',-' . get_cat_ID( $cat_name='SecondCat' );
    $catIDs .= ',-' . get_cat_ID( $cat_name='ThirdCat' );

    I just put the minus sign “-” beside the coma. Now it’s works perfectly.

    use this <?php query_posts('category_name=array('Nyheter', 'Movie')&showposts=4'); ?>

    This doesn’t work for excluding categories.

    query_posts(array('category__not_in' => array('Nyheter', 'Movie')));


    query_posts('cat' => '-2,-6,-10');
    # category__not_in – must use cat ids

    If you want to exclude cats by ID it’s ok, but if you want to exclude by name, @stvwlf solution works perfectly.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Query with multiple category_name?’ is closed to new replies.