4

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

  • This is how i do it which is tested and works wordpress.stackexchange.com/a/134977/9884 – Brad Dalton Feb 28 '14 at 12:49
  • @BradDalton Hi, thanks for your reply! I've done that, and archives do work, but I want to get the taxonomies working. For instance I need to be able to browse to "example.com/promotion/loreal" and only get the posts that are set to that particular term. Your code does not do that, or did I misunderstand it? – Jonathan Feb 28 '14 at 13:02
  • That code adds the settings which enable you to create unlimited taxonomy terms. Please check the new admin menu links under the CPT and let me know if its what you wanted. – Brad Dalton Feb 28 '14 at 13:14
  • Try changing the Permalink settings (Settings → Permalinks) from whatever setting you're using now to another option, saving the settings, then switching back to your original permalink settings. This will cause Wordpress to rebuild the cached rewrite rules. I just had the same problem and I believe this step fixed the issue for me… – thirdender Feb 28 '14 at 21:19
  • Hi @thirdender, I've flushed the permalinks several times now but that doesn't seem to be my problem. I can get to the taxonomy term, no problem, but it doesn't find any posts and the $wp_query->posts is blank, even though I have at least 12 custom posts linked to the taxonomy term. – Jonathan Mar 1 '14 at 0:26
7

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!

  • 1
    This was a life saver! Thank you! So weird by default WP does not show any results in archives page if your taxonomy is only associated with a custom post type. Not cool. – Trevor Feb 18 '16 at 5:20
  • This solution either didn't work for me! – Yazid Erman Feb 25 '17 at 14:19
0

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;
}

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.