WordPress.org

Ready to get started?Download WordPress

Forums

'category'=>get_cat_ID HELP (6 posts)

  1. yortem
    Member
    Posted 11 months ago #

    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?

  2. vtxyzzy
    Member
    Posted 10 months ago #

    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.

  3. yortem
    Member
    Posted 10 months ago #

    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?

  4. vtxyzzy
    Member
    Posted 10 months ago #

    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)); ?>
  5. yortem
    Member
    Posted 10 months ago #

    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.

  6. vtxyzzy
    Member
    Posted 10 months ago #

    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);

Reply

You must log in to post.

About this Topic

Tags

No tags yet.