Support » Plugin: Custom Post Type UI » Custom Post Type linked with WP Core category taxonomy problem

  • Resolved ërralb

    (@ierpe)


    You can find more details about the problem on this Stackexchange question.

    Basically, when doing 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);

    while “cat” is a WP Core category taxonomy id and “projets” a custom post type created with your plugin, it seems that the “post_type” is ignored and WP_Query returns all posts (regular “post” posts as well as “projets” posts) that are linked with the category.
    When in fact I only want “projets” post types that have the wanted category.

    Could you help?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    An initial thought while I still take some stabs at helping figure out a solution.

    We don’t do anything regarding the WP_Query class, so this is probably something internal to WordPress more than anything we’ve done.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    As is, and changing only minor details like post type slug and category ID, I’m not managing to recreate issues with mixing of posts and the custom post type. Each time I’m only getting the post type specified and nothing with “post” post type.

    Are there other parts of your website that are interacting with query arguments through things like pre_get_posts that may be unintentionally affecting this query here?

    Thanks for the answer.

    Indeed I am using this in the theme file:

    //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' );

    But I want this post type to be included in the loop, and the problem occurs only when I call WP_Query with a category… weird isn’t it? Or am I doing something wrong in this function?

    Adding $args['suppress_filters'] = true; before calling the WP_Query solved it… Not sure if it’s the way it should be though

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Does your custom query above work when this one is not running? If yes, then I think we have our culprit.

    That said, and I’m pretty confident with this thought as well, we need to amend that 2nd snippet slightly.

    I cleaned things up slightly, and added some more details, including comments.

    <?php
    //Add projets custom posts within the loop
    function namespace_add_custom_types( $query ) {
    	// We only want this affecting queries on the frontend.
    	if ( is_admin() ) {
    		return $query;
    	}
    
    	// We only want this affecting what's considered the "main query" which is going to be the category and tag archive.
    	// Where we're "failing", is with the custom WP_Query call you're doing, which isn't the main query. Thus why you were
    	// getting both types below as well. This check will prevent that.
    	if ( ! $query->is_main_query() ) {
    		return $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' );
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom Post Type linked with WP Core category taxonomy problem’ is closed to new replies.