I have created a custom post type called "community", created a new post with a category (cat id = 6). I am using the following code:

$args = array (
    'numberposts' => -1,
    'post_type'   => 'community',
    'post_status' => 'publish',
    'orderby'     => 'date',
    'order'       => 'DESC',
    'category'    => 6
$communities = get_posts($args);

It worked perfectly inside my page.php. However, it's not working when I am trying the same inside category.php page, I am not getting the posts accurately! It's returning all the other posts.

Any idea?

  • Do not run custom queries in place of the main query on the category archive page. Use pre_get_posts to alter the main query – Pieter Goosen Nov 22 '15 at 16:49

My guess is that there is a filter-- pre_get_posts most likely-- being applied to the category archive and that that filter is written too broadly, and applies to all queries on the page instead of just the one it is intended for-- probably the main query. The correct solution would be to fix that filter, but I can't say exactly how without seeing the code. I'd guess you need to add if(is_main_query()) {... } but again, that is guessing.

You could remove the filter if you know what its callback is:


You would need that just before your query and you might need an add_filter() afterwards, depending on your page structure.

You could also use:

remove_all_filters( 'pre_get_posts` );

But that is really taking a sledge hammer to the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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