2

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.

9
  • 1
    Have you registered native categories for "projets" CPT? Do you have any actions modifying query assigned to pre_get_posts hook? 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
    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
    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... 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
    Jan 29, 2019 at 18:55

3 Answers 3

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).
1
  • 1
    A call to pre_get_posts in my theme files was the culprit
    – Pierre
    Jan 30, 2019 at 8:37
2

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);
4
  • As I said in a previous comment, I already tried it with the tax_query and the result is the same. I tried nevertheless to copy / paste your code and got the same problem...
    – Pierre
    Jan 29, 2019 at 14:51
  • 1
    are you using any pre_get_posts hook in your theme file? Jan 29, 2019 at 15:00
  • you're right, I am using it and commenting it fixes the problem... kind of, since if I comment this code out then the custom post isn't added to the loop anymore and will not appear in the "all posts" loop. Any idea?
    – Pierre
    Jan 29, 2019 at 18:35
  • I updated my question with the code.
    – Pierre
    Jan 29, 2019 at 18:46
0

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

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

7
  • I think you can, it's working : codex.wordpress.org/Class_Reference/… but the problem is not here, maybe I didn't explain my problem correctly. The category selection is working, but the WP_Query does not returns only "projets" post types... But anyway I tried with tax_query and the result is the same. Instead of having only posts with the post_type "projets", I also get posts with the post_type "post"
    – Pierre
    Jan 25, 2019 at 13:11
  • Well, of course you can. But it will make sense only if you register native categories for other CPTs. Jan 28, 2019 at 10:49
  • Yes sure, but I think he has registered a custom post type. Jan 28, 2019 at 11:53
  • So what? You can register built-in taxonomies for CPTs also... Jan 28, 2019 at 14:39
  • Sorry, I mean custom taxonomy. Jan 29, 2019 at 10:12

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.