0

I have CPT and Taxonomy (code below). When viewing taxonomy term archive page, it's empty. WordPress doesn't know it has include my custom post type to WP_Query. Am I doing something wrong?

function register_cpt() {

    // POST TYPE
    $args = array(
        'label'               => 'Photogallery',
        'supports'            => array( 'title', 'editor',),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => false,
        'show_in_admin_bar'   => false,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => true,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'taxonomies'                    => array('gallery_cat')
    );
    register_post_type( 'photogallery', $args );

    // TAXONOMY
    $args = array(
        'label'                     => 'Photogallery categories',
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => false,
        'show_tagcloud'              => false,
    );
    register_taxonomy( 'gallery_cat', 'photogallery', $args );

    // CONNECTING ...
    register_taxonomy_for_object_type( 'gallery_cat', 'photogallery' );
}
add_action( 'init', 'register_cpt');

When visiting example.com/gallery_cat/cat-1, it's empty. But there is one item with this term in back-end.

When I check main query, its SQL is this:

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

As clearly seen, my custom post type "photogallery" is not included.

What am I doing wrong? Is there some misconfiguration in my code?

0

when registering the post type , try 'exclude_from_search' => false,

  • Oh god! Now I see the notice... but why, WordPress, why??? Thank you, Abad! – Marek Aug 27 '15 at 17:43
  • Thanks for this. Was scratching my head for ages before I found this solution. Problem is my client doesn't want the CPT to show up in search but they DO want it to have a custom tax. I guess I'll have to pre_get_posts to solve that one... Just one of WP's many annoying quirks. – powerbuoy Jul 12 '16 at 1:18

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.