I have a custom loop that shows 120 posts in total on a certain page. The posts are from several categories. That works perfectly. But I need to change it.

I need:

  • 120 posts only of the category 2
  • all posts of other cats can be displayed without any limitation. Is this possible?

My loop goes like this:

            global $post;
            $tmp_post = $post;
            $args = array( 
            'numberposts' => '120',
            'orderby' => 'date',
            'order'   => 'DESC',
            'cat' => array(1,2,7,8,9,27, 30, 31),
            'tax_query' => array(
                            'taxonomy' => 'post_format',
                            'field' => 'slug',
                            'terms' => array( 'post-format-aside','post-format-video' ),
                            'operator' => 'NOT IN',
            $myposts = get_posts( $args );
            foreach( $myposts as $post ) : setup_postdata($post); 


[EDIT] I updated the question in order to make it more clear. [/EDIT]

  • yes, just ask for 2, instead o 1,2,7,8,9,27,30, and 31. Also note that the NOT IN tax_query is ultra expensive and very slow/heavy on the database
    – Tom J Nowell
    Dec 10, 2020 at 16:58
  • And what, if I need to ask for 1, 2, 7, 8, 9... (?) That is the given code, I cannot change it as I want.
    – rabo
    Dec 10, 2020 at 19:54
  • I don't understand. If you want to make changes you have to change the code. Why can't you change the code?
    – Tom J Nowell
    Dec 10, 2020 at 23:52
  • Off course I have to change the code. ;) But the solution to kick out some categories is not a real solution. I want the code as it is, but the output of cat 2 (and only cat 2!) should be stopped after a max of 120 posts.
    – rabo
    Dec 11, 2020 at 9:23
  • I see, that's not what your question asked, can you edit your question to make that clearer? Right now what your question says and what you want, are not the same
    – Tom J Nowell
    Dec 11, 2020 at 12:52

1 Answer 1


If you only want posts from the category with ID 2, that's easy, only ask for category 2:

'cat' => array( 2 ),
  • No, I want it as it is. but instead of a maximum of 120 in total I want a maximum of 120 of cat 2. The other cats should be without a maximum, so all posts should be shown.
    – rabo
    Dec 11, 2020 at 9:22
  • That's not what your question asks, you should update your question to reflect what you actually want
    – Tom J Nowell
    Dec 11, 2020 at 12:53
  • I updated the question now. Hope it is more clear now.
    – rabo
    Dec 14, 2020 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.