Custom Taxonomy with Custom Post Type Finds No Posts

I’ve created a custom post type named mfg_ppn and a custom taxonomy called mfg_pp, and they are referenced as $this->cpt_promotion_newsand $this->cpt_promotionin case you were wondering about them.

Everything looks good in the admin, and I can easily assign the taxonomy to the custom posts. But when I visit my site using http://example.com/promotion/company I do not get any posts, even though there are 12 posts bound to that particular term.

Here is my PHP code used for registering the post type:

// Register campaign news:
register_post_type($this->cpt_promotion_news, array(
    'labels' => array(
        'name' => __('Campaign Posts', $this->plugin_locale),
        'singular_name' => __('Promotion Campaign', $this->plugin_locale),
        'menu_name' => __('Promotion', $this->plugin_locale),
        'all_items' => __('All Campaign Posts', $this->plugin_locale),
        'add_new' => __('Add New Post', $this->plugin_locale),
        'add_new_item' => __('Add New Campaign Post', $this->plugin_locale),
        'edit_item' => __('Edit Post', $this->plugin_locale),
        'new_item' => __('New Post', $this->plugin_locale),
        'view_item' => __('View Post', $this->plugin_locale),
        'search_items' => __('Search Posts', $this->plugin_locale),
        'not_found' => __('No posts found', $this->plugin_locale),
        'not_found_in_trash' => __('No posts found in trash', $this->plugin_locale),
        'parent_item_colon' => __('Post parent:', $this->plugin_locale)
    ),
    'description' => __('News for a partner promotion campaign.', $this->plugin_locale),
    'public' => true,
    'exclude_from_search' => true,
    'show_in_nav_menus' => true,
    'menu_position' => 25,
    'show_in_menu' => true,
    'map_meta_cap' => true,
    'capabilities' => array( 'manage_options' ),
    'capability_type' => 'post',
    'hierarchical' => false,
    'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
    'taxonomies' => array( $this->cpt_promotion ),
    'has_archive' => true,
    'rewrite' => array(
        'slug' => __('promotion-post', $this->plugin_locale)
    )
));

And here is the code for registering the taxonomy:

// Register promotion campaigns:
register_taxonomy($this->cpt_promotion, $this->cpt_promotion_news, array(
    'labels' => array(
        'name' => __('Promotion Campaigns', $this->plugin_locale),
        'singular_name' => __('Promotion Campaign', $this->plugin_locale),
        'menu_name' => __('All Campaigns', $this->plugin_locale),
        'all_items' => __('All Campaigns', $this->plugin_locale),
        'edit_item' => __('Edit Campaign', $this->plugin_locale),
        'view_item' => __('View Campaign', $this->plugin_locale),
        'update_item' => __('Update Campaign', $this->plugin_locale),
        'add_new_item' => __('Add New Promotion Campaign', $this->plugin_locale),
        'new_item_name' => __('New Campaign Name', $this->plugin_locale),
        'parent_item' => __('Post parent:', $this->plugin_locale),
        'parent_item_colon' => __('Post parent:', $this->plugin_locale),
        'search_items' => __('Search Campaigns', $this->plugin_locale),
        'add_or_remove_items' => __('Add or Remove Campaigns', $this->plugin_locale)
    ),
    'public' => true,
    'show_ui' => true,
    'show_in_nav_menus' => false,
    'show_tagcloud' => false,
    'show_admin_column' => true,
    'hierarchical' => true,
    'query_var' => true,
    'rewrite' => array(
        'slug' => __('promotion', $this->plugin_locale)
    )
));

I have also created a template called taxonomy-mfg_pp, which is the name of the taxonomy, but it never outputs any post, even though there are posts. The culprit seems to be that the WP_Query executed only looks for posts, pages and attachments, as seen below:

SELECT SQL_CALC_FOUND_ROWS  qADrathuFrU2_posts.ID FROM qADrathuFrU2_posts  INNER JOIN qADrathuFrU2_term_relationships ON (qADrathuFrU2_posts.ID = qADrathuFrU2_term_relationships.object_id) WHERE 1=1  AND ( qADrathuFrU2_term_relationships.term_taxonomy_id IN (1196) ) AND qADrathuFrU2_posts.post_type IN ('post', 'page', 'attachment') AND (qADrathuFrU2_posts.post_status = 'publish' OR qADrathuFrU2_posts.post_author = 1 AND qADrathuFrU2_posts.post_status = 'private') AND qADrathuFrU2_posts.post_password = ''  GROUP BY qADrathuFrU2_posts.ID ORDER BY qADrathuFrU2_posts.post_date DESC LIMIT 0, 10

Am I doing something wrong and am I going about this the wrong way?

Please help me as the deadline is approaching and I just can’t seem to get this right!

Thanks in advance,
Jonathan

Solutions Collecting From Web of "Custom Taxonomy with Custom Post Type Finds No Posts"

I don’t know how or why, but this code below solved by issue. Seems to me like I shouldn’t need it, but apparently I do.

add_filter('pre_get_posts', array(&$this, 'modify_pre_query_request'));
public function modify_pre_query_request($query){
    if ($query->is_main_query()){
        if ($query->is_tax){
            $post_type = get_query_var('post_type');
            if (!$post_type){
                $post_type = array( 'post', 'YOUR POST TYPE' );
                $query->set('post_type', $post_type);
            }
        }
    }
}

Keep in mind that the above code is for an object-oriented theme, if you do not work on an object-oriented theme, use the following code instead:

add_filter('pre_get_posts', 'modify_pre_query_request');
function modify_pre_query_request($query){
    if ($query->is_main_query()){
        if ($query->is_tax){
            $post_type = get_query_var('post_type');
            if (!$post_type){
                $post_type = array( 'post', 'YOUR POST TYPE' );
                $query->set('post_type', $post_type);
            }
        }
    }
}

Thanks for all the help!

With all appreciation to Jonathan’s answer and thanks to this answer, this might be a more flexible way to fix this issue, because it automatically determines the post type and you won’t need to worry about the post type name :

add_action( 'pre_get_posts', 'dw_handle_posts' );
function dw_handle_posts( $query ) {
    if( ! $query->is_main_query() || is_admin() )
        return;

    if ( $query->is_tax ){
        $post_type = get_query_var('post_type');
        if( ! $post_type ){
            global $wp_taxonomies;
            $taxo = get_queried_object();
            $post_type = ( isset( $wp_taxonomies[$taxo->taxonomy] ) ) ? $wp_taxonomies[$taxo->taxonomy]->object_type : array();
            $query->set('post_type', $post_type);
        }
    }

    return $query;
}