wp query - WP_Query with custom post_type and cat retrieving unwanted posts with the custom posts

admin2025-06-03  4

I have a custom post type "projets" that is using the category taxonomy (so it is sharing categories with regular posts).

When I call WP_Query like this:

$args = array(
    'post_type' => 'projets',
    'posts_per_page' => 6,
    'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
);
$projects = new WP_Query($args);

It works perfectly fine. But when I add a category to the arguments like this:

$args = array(
    'post_type' => 'projets',
    'posts_per_page' => 6,
    'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
    'cat' => 39
);
$projects = new WP_Query($args);

It does not returns only projets custom post_type but also regular posts that share the category 39... Is there a way to make sure only the custom post type will be returned?

For info I use the Custom Post Type UI extension to declare the post type, I did not declare them myself.


Edit:

To clarify after the first comments, I did not register a custom taxonomy.

I only registered a custom post type called "projets" using the CPT UI plugin, with which I linked the Categories (WP Core) built-in taxonomy.

You can have a look at this and this screenshots to see how I configured everything.

The post type name I am using with WP_Query is correct, since it does return the "projets" posts. The problem is that when I add a category to the $args, then regular posts (posts with the "post" type) are being returned.


Edit 2 and solution:

As pointed out by @krzysiek-dróżdż and @chinmoy-kumar-paul , in my theme file I am using the pre_get_posts filter :

//Add projets custom posts within the loop
function namespace_add_custom_types( $query ) {
  if( (is_category() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'projets'
        ));
    }
    return $query;
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

If I comment it out, the problem is gone... So the problem is found out!

But isn't it a WordPress bug? I mean I still do want this custom post type to be included in the loop, so my problem is not completely solved.

In the end, one solution is to add $args['suppress_filters'] = true; before doing the query, allowing to avoid this filter and fix the problem.

I have a custom post type "projets" that is using the category taxonomy (so it is sharing categories with regular posts).

When I call WP_Query like this:

$args = array(
    'post_type' => 'projets',
    'posts_per_page' => 6,
    'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
);
$projects = new WP_Query($args);

It works perfectly fine. But when I add a category to the arguments like this:

$args = array(
    'post_type' => 'projets',
    'posts_per_page' => 6,
    'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
    'cat' => 39
);
$projects = new WP_Query($args);

It does not returns only projets custom post_type but also regular posts that share the category 39... Is there a way to make sure only the custom post type will be returned?

For info I use the Custom Post Type UI extension to declare the post type, I did not declare them myself.


Edit:

To clarify after the first comments, I did not register a custom taxonomy.

I only registered a custom post type called "projets" using the CPT UI plugin, with which I linked the Categories (WP Core) built-in taxonomy.

You can have a look at this and this screenshots to see how I configured everything.

The post type name I am using with WP_Query is correct, since it does return the "projets" posts. The problem is that when I add a category to the $args, then regular posts (posts with the "post" type) are being returned.


Edit 2 and solution:

As pointed out by @krzysiek-dróżdż and @chinmoy-kumar-paul , in my theme file I am using the pre_get_posts filter :

//Add projets custom posts within the loop
function namespace_add_custom_types( $query ) {
  if( (is_category() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'projets'
        ));
    }
    return $query;
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

If I comment it out, the problem is gone... So the problem is found out!

But isn't it a WordPress bug? I mean I still do want this custom post type to be included in the loop, so my problem is not completely solved.

In the end, one solution is to add $args['suppress_filters'] = true; before doing the query, allowing to avoid this filter and fix the problem.

Share Improve this question edited Jan 29, 2019 at 18:59 Pierre asked Jan 25, 2019 at 10:32 PierrePierre 9510 bronze badges 9
  • 1 Have you registered native categories for "projets" CPT? Do you have any actions modifying query assigned to pre_get_posts hook? – Krzysiek Dróżdż Commented Jan 28, 2019 at 10:51
  • 1 Yes within the Custom Post Type UI plugin I linked the native categories with the "projet" CPT – Pierre Commented Jan 28, 2019 at 14:37
  • Are you certain that the singular name for your CPT is "projets" and not "projet"? You need to use the singular name as your post_type argument. – WebElaine Commented Jan 29, 2019 at 14:16
  • 1 @WebElaine not singular name, but slug, to be precise ;) Singular name is just a label. But it may be a good catch... – Krzysiek Dróżdż Commented Jan 29, 2019 at 14:25
  • 1 @KrzysiekDróżdż I didn't pay enough attention to your first comment, the problem was linked to the pre_get_posts filter – Pierre Commented Jan 29, 2019 at 18:55
 |  Show 4 more comments

3 Answers 3

Reset to default 1 +50

Assuming that you have a CPT with slug projets and you've registered built-in categories for that CPT, it all looks correct.

So there are two possibilities, that I would check:

  1. Typos - maybe you misstyped your CPT name somewhere in your code.
  2. pre_get_posts filter is modifying your query and changing its behavior (it's pretty common if you write pre_get_posts and don't pay enough attention to conditions inside of it).

You will use the tax_query option for custom post type. So your query will be look like this

$args = array(
    'post_type' => 'projets',
    'posts_per_page' => 6,
    'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => array( 39 )
        )
    ),
);
$projects = new WP_Query($args);

You can not pass CAT parameter for the custom taxonomy. you have to use tax_query.

Reference WordPress codex URL: https://codex.wordpress/Class_Reference/WP_Query#Taxonomy_Parameters

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748963891a315205.html

最新回复(0)