Support » Fixing WordPress » 'category'=>get_cat_ID HELP

  • i have this code :
    <?php $catPost = get_posts(array('category'=>get_cat_ID( '' ),'numberposts'=>5)); ?>

    how i can i do that all the categories will show except one category that i choose?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Have you tried this:

    <?php $catPost = get_posts(array('cat'=>'-32'), 'numberposts'=>5)); ?>

    Replace the 32 with the id of the cat you want to exclude.

    yes, its worked.
    but i have another problem, i have post in 2 categorys… so if i disable one category the post still show becouse the other category he belong to.
    do you have any idea what to do?

    You said you only wanted to exclude one category. If you want to exclude more than one, add the ids to the list:

    <?php $catPost = get_posts(array('cat'=>'-32,-44,-58'), 'numberposts'=>5)); ?>

    i know. but for example i have a post in category 2 and 3…
    i disable the category 2 but the post is also in category 3 and he show on the list. category 3 is important and i dont want to disable it.

    In that case you will need to use the array form of arguments with a taxonomy query. This is untested, but it should be close:

    $args = array(
       'number_posts' => 5,
       'tax_query' => array(
          'relation' => 'AND',
          array(
             'taxonomy' => 'category',
             'field' => 'id',
             'terms' => 3,
             ),
          array(
             'taxonomy' => 'category',
             'field' => 'id',
             'terms' => array(2),
             'operator' => 'NOT IN'
             )
       ),
    );
    
    $catPost = get_posts($args);
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘'category'=>get_cat_ID HELP’ is closed to new replies.