In my single.php file I am using a nested WP_Query to show more posts, at the bottom of the page. My code for the query and nested loop are as follows:


    $ep_args = array(
        'post_type'       =>    'post',
        'category_not_in' =>    93,
        'orderby'         =>    'rand',
        'posts_per_page'  =>    4,
        'offset'          =>    1,

    $ep_query = new WP_Query( $ep_args );

    while ($ep_query->have_posts()) : $ep_query->the_post();

        if( $post->ID == $do_not_duplicate ) continue;

    get_template_part( 'content', 'postthumb' ); 



I have tried using both 'category_name' (with the desired category's slug) and 'category_not_in' (with the undesired category's ID), and in both cases I am getting posts from the undesired category. Furthermore, I'm finding that using 'orderby' => 'rand' will only result in 3 posts instead of 4, if I do not set the offset to 1.

How can I get my query to result in 4 posts from one specific category, and no other categories?

  • Are these undesired categories sub-categories? You shouldn't hardcode term IDs like that. You should also wrap your loop in an if ( $ep_query->have_posts() ) to prevent reseting the post data if it was never set
    – Tom J Nowell
    Apr 26, 2015 at 18:27
  • They are not sub-categories.
    – Codette
    Apr 26, 2015 at 19:00

1 Answer 1


Use this one-

'category__not_in' => array( 93, 94, 95 ),

instead of-

'category_not_in' => 93,

Note: The term is category__not_in, not category_not_in. Use 2 underscores after category.

Edit: You are using-

'offset' => 1

That forces to show posts from 2nd one (skips the first post). So, it's showing one post less! If you use 'offset' => 3, then it will start from 4th post. Hope you understand.

  • This gets rid of the post from the undesired category, however I now only have 3 posts showing, instead of 4.
    – Codette
    Apr 26, 2015 at 20:26
  • I have edited my answer.
    – mukto90
    Apr 26, 2015 at 22:11
  • Have you verified there are actually 4 posts to show? With the offset field, you'll need there to be 5 posts for 4 to show
    – Tom J Nowell
    Apr 26, 2015 at 22:11
  • I have 7 posts in the desired category (9 posts total), more than enough. I should be able to offset by 1 and still get 4 posts from my query, not 3. Update: I have edited my query to remove the offset (I initially thought it was necessary since I was using if( $post->ID == $do_not_duplicate ) continue; to keep the current post from showing up in my query, but now it doesn't seem to be necessary). I now have simply 'post_type' = 'post', 'category_name' = 'episodes', 'orderby' = 'rand', and 'posts_per_page' = 4, and now sometimes I get 4 posts, but a lot of times I only get 3 posts.
    – Codette
    Apr 26, 2015 at 23:19

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.