1

I have the file taxonomy-project.php and am trying to view results for some of my terms.

I keep getting a result of NO even though I have a post applied to that particular term.

taxonomy-project.php

<?php get_header(); ?>

    <section>
        <div class="container">
            <div class="row">
                <div class="col-12">

                    <?php if(have_posts()) : ?>
                        <div class="fancybox">
                            <div class="row">
                                <?php while(have_posts()) : the_post(); ?>
                                    YES
                                <?php endwhile; ?>
                            </div>
                        </div>

                    <?php else : ?>
                        NO
                    <?php endif; ?>

                </div>
            </div>


        </div>
    </section>


<?php get_footer (); ?>

Functions.php

    // register custom posts
add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'work',
        array(
            'labels' => array(
            'name' => __( 'Work' ),
            'singular_name' => __( 'Work' ),
            'add_new_item' => __( 'Add New Work' ),
            'edit_item' => __( 'Edit Work' ),
            'new_item' => __( 'New Work' ),
            'view_item' => __( 'View Work' ),
            'search_items' => __( 'Search Work' )
            ),
            'public' => true,
            'exclude_from_search' => true,
            'menu_position' => 5,
            'supports' => array('title', 'thumbnail'),
            'taxonomies' => array('project')
        )
    );
}


// register custom taxonomies
 function taxonomyRegister_init() {
   // create a new taxonomy
   register_taxonomy(
     'project',
     'work',
     array(
       'label' => __('Project'),
       'sort' => true,
       'args' => array('orderby' => 'term_order'),
       'hierarchical' => true
     )
   );
 }
add_action( 'init', 'taxonomyRegister_init' );

Query

    WP_Query Object
    (
        [query] => Array
            (
                [project] => lorem-ipsum
            )
    
        [query_vars] => Array
            (
                [project] => lorem-ipsum
                [error] => 
                [m] => 
                [p] => 0
                [post_parent] => 
                [subpost] => 
                [subpost_id] => 
                [attachment] => 
                [attachment_id] => 0
                [name] => 
                [pagename] => 
                [page_id] => 0
                [second] => 
                [minute] => 
                [hour] => 
                [day] => 0
                [monthnum] => 0
                [year] => 0
                [w] => 0
                [category_name] => 
                [tag] => 
                [cat] => 
                [tag_id] => 
                [author] => 
                [author_name] => 
                [feed] => 
                [tb] => 
                [paged] => 0
                [meta_key] => 
                [meta_value] => 
                [preview] => 
                [s] => 
                [sentence] => 
                [title] => 
                [fields] => 
                [menu_order] => 
                [embed] => 
                [category__in] => Array
                    (
                    )
    
                [category__not_in] => Array
                    (
                    )
    
                [category__and] => Array
                    (
                    )
    
                [post__in] => Array
                    (
                    )
    
                [post__not_in] => Array
                    (
                    )
    
                [post_name__in] => Array
                    (
                    )
    
                [tag__in] => Array
                    (
                    )
    
                [tag__not_in] => Array
                    (
                    )
    
                [tag__and] => Array
                    (
                    )
    
                [tag_slug__in] => Array
                    (
                    )
    
                [tag_slug__and] => Array
                    (
                    )
    
                [post_parent__in] => Array
                    (
                    )
    
                [post_parent__not_in] => Array
                    (
                    )
    
                [author__in] => Array
                    (
                    )
    
                [author__not_in] => Array
                    (
                    )
    
                [ignore_sticky_posts] => 
                [suppress_filters] => 
                [cache_results] => 1
                [update_post_term_cache] => 1
                [lazy_load_term_meta] => 1
                [update_post_meta_cache] => 1
                [post_type] => 
                [posts_per_page] => 10
                [nopaging] => 
                [comments_per_page] => 50
                [no_found_rows] => 
                [taxonomy] => project
                [term] => lorem-ipsum
                [order] => DESC
            )
    
        [tax_query] => WP_Tax_Query Object
            (
                [queries] => Array
                    (
                        [0] => Array
                            (
                                [taxonomy] => project
                                [terms] => Array
                                    (
                                        [0] => lorem-ipsum
                                    )
    
                                [field] => slug
                                [operator] => IN
                                [include_children] => 1
                            )
    
                    )
    
                [relation] => AND
                [table_aliases:protected] => Array
                    (
                        [0] => wp_term_relationships
                    )
    
                [queried_terms] => Array
                    (
                        [project] => Array
                            (
                                [terms] => Array
                                    (
                                        [0] => lorem-ipsum
                                    )
    
                                [field] => slug
                            )
    
                    )
    
                [primary_table] => wp_posts
                [primary_id_column] => ID
            )
    
        [meta_query] => WP_Meta_Query Object
            (
                [queries] => Array
                    (
                    )
    
                [relation] => 
                [meta_table] => 
                [meta_id_column] => 
                [primary_table] => 
                [primary_id_column] => 
                [table_aliases:protected] => Array
                    (
                    )
    
                [clauses:protected] => Array
                    (
                    )
    
                [has_or_relation:protected] => 
            )
    
        [date_query] => 
        [queried_object] => WP_Term Object
            (
                [term_id] => 2
                [name] => Lorem ipsum
                [slug] => lorem-ipsum
                [term_group] => 0
                [term_taxonomy_id] => 2
                [taxonomy] => project
                [description] => 
                [parent] => 0
                [count] => 1
                [filter] => raw
            )
    
        [queried_object_id] => 2
        [request] => SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1  AND ( 
      wp_term_relationships.term_taxonomy_id IN (2)
    ) AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled') GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order, wp_posts.post_date DESC LIMIT 0, 10
        [posts] => Array
            (
            )
    
        [post_count] => 0
        [current_post] => -1
        [in_the_loop] => 
        [comment_count] => 0
        [current_comment] => -1
        [found_posts] => 0
        [max_num_pages] => 0
        [max_num_comment_pages] => 0
        [is_single] => 
        [is_preview] => 
        [is_page] => 
        [is_archive] => 1
        [is_date] => 
        [is_year] => 
        [is_month] => 
        [is_day] => 
        [is_time] => 
        [is_author] => 
        [is_category] => 
        [is_tag] => 
        [is_tax] => 1
        [is_search] => 
        [is_feed] => 
        [is_comment_feed] => 
        [is_trackback] => 
        [is_home] => 
        [is_privacy_policy] => 
        [is_404] => 
        [is_embed] => 
        [is_paged] => 
        [is_admin] => 
        [is_attachment] => 
        [is_singular] => 
        [is_robots] => 
        [is_favicon] => 
        [is_posts_page] => 
        [is_post_type_archive] => 
        [query_vars_hash:WP_Query:private] => c087b9cf5d8d955411d9eb52431cd969
        [query_vars_changed:WP_Query:private] => 
        [thumbnails_cached] => 
        [stopwords:WP_Query:private] => 
        [compat_fields:WP_Query:private] => Array
            (
                [0] => query_vars_hash
                [1] => query_vars_changed
            )
    
        [compat_methods:WP_Query:private] => Array
            (
                [0] => init_query_flags
                [1] => parse_tax_query
            )
)
5
  • What's your global $wp_query show? Have you resaved permalinks? – Howdy_McGee Nov 3 '20 at 21:03
  • @Howdy_McGee yep I've tried resaving my permalinks but no luck — I've just updated my question now with my wp_query – user39214 Nov 3 '20 at 21:37
  • If you manually set the post type in pre_get_posts ( checking if it's is_tax( 'project' ) does it then appear? – Howdy_McGee Nov 3 '20 at 21:39
  • @Howdy_McGee Im struggling to get my head around pre_get_posts sorry! – user39214 Nov 3 '20 at 22:27
  • @Howdy_McGee I've found the problem! have just posted an answer – user39214 Nov 3 '20 at 22:32
1

I've found the problem, and now I realise I had this exact same problem years ago on a different project:

This:

'exclude_from_search' => true

Needed changing to:

'exclude_from_search' => false
'has_archive' => true,

I don't get why excluding a post type from search should affect archive templates, but at least it's fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy