I have a custom taxonomy assigned to a custom post type.Iâve created a template called âtaxonomy-brands.phpâ, which gets called when I visit a custom taxonomy archive, however when I call the loop in this template, it doesnât think I have any posts (it skips the if(have_posts()) and executes the else {}).My custom post type has âhas_archiveâ => true, and when I go to the archive page for the post type I see the archive, itâs just the taxonomy archives that donât work.
Is there something in the code that might be causing this?
Thanks in advance for your help.
Here is the code i used to register my CPT.
add_action( 'init', 'register_custom_post_type_apknews' );
function register_custom_post_type_apknews() {
$phone_labels = array(
'name' => _x( 'specification', 'specification' ),
'singular_name' => _x( 'specification', 'specification' ),
'add_new' => _x( 'Add New', 'specification' ),
'add_new_item' => _x( 'Add New specification', 'specification' ),
'edit_item' => _x( 'Edit specification', 'specification' ),
'new_item' => _x( 'New specification', 'specification' ),
'view_item' => _x( 'View specification', 'specification' ),
'search_items' => _x( 'Search specification', 'specification' ),
'not_found' => _x( 'No specification found', 'specification' ),
'not_found_in_trash' => _x( 'No specification found in Trash', 'specification' ),
'parent_item_colon' => _x( 'Parent specification:', 'specification' ),
'menu_name' => _x( 'specifications', 'specification' ),
);
$phone_args = array(
'labels' => $phone_labels,
'hierarchical' => false,
'description' => 'All mobile phone specifications.', // description for custom post type
'supports' => array( 'title','thumbnail'), // more support : 'excerpt','author','trackbacks','custom-fields','comments','revisions','page-attributes','post-formats'
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => get_stylesheet_directory_uri(). '/images/specification.png',
'show_in_nav_menus' => false,
'publicly_queryable' => true,
'exclude_from_search' => true,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true, //array( 'slug' => 'specif' ),
'capability_type' => 'post'
);
register_post_type( 'specification', $phone_args );
}
And here is the code I used to register my custom taxonomy.
add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );
function create_topics_hierarchical_taxonomy() {
$brands_labels = array(
'name' => _x( 'Brands', 'Brands' ),
'singular_name' => _x( 'Brand', 'Brand' ),
'search_items' => __( 'Search Brands' ),
'all_items' => __( 'All Brands' ),
'parent_item' => __( 'Parent Brand' ),
'parent_item_colon' => __( 'Parent Brand:' ),
'edit_item' => __( 'Edit Brand' ),
'update_item' => __( 'Update Brand' ),
'add_new_item' => __( 'Add New Brand' ),
'new_item_name' => __( 'New Brand Name' ),
'menu_name' => __( 'Brands' ),
);
register_taxonomy('brands',array('specification'), array(
'hierarchical' => true,
'labels' => $brands_labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'brands' ),
));
}
And finally here is the code in ‘taxonomy-brands.php‘ .
<?php
$args = array(
'post_type' => 'specification',
'tax_query' => array(
array(
'taxonomy' => 'brands',
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<!--My stuff-->
<div class="fix single_content floatleft">
<a href="<?php the_permalink();?>"><?php the_post_thumbnail('specification-home-feature-img', array('alt' => get_the_title())); ?></a>
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<p>Tk: 8900 BDT</p>
</div>
<!--My stuff-->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
i think the problem is in your query, you need to add terms in order to work.