Custom posts not showing when query has custom taxonomy inside
-
I have created a custom post type and custom taxonomy to go with it.
However, when I write the query to show posts from that CPT and has that taxonomy included nothing shows. Remove the taxonomy section of code and it shows posts from the CPT.
My CPT code is;
add_action("init", "custom_posttype_menu_wp_admin1"); function custom_posttype_menu_wp_admin1() { register_post_type('products', array('label' => __('Products','templatic'), 'labels' => array( 'name' => __('Products','templatic'), 'singular_name' => __('Products','templatic'), 'add_new' => __('Add Products','templatic'), 'add_new_item'=> __('Add New Products','templatic'), 'edit' => __('Edit','templatic'), 'edit_item'=> __('Edit Products','templatic'), 'new_item' => __('New Products','templatic'), 'view_item' => __('View Products','templatic'), 'search_items' => __('Search Products','templatic'), 'not_found' => __('No Products','templatic'), 'not_found_in_trash'=> __('No Products found in trash','templatic') ), 'public' => true, 'can_export' => true, 'show_ui' => true, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'menu_icon' => 'dashicons-admin-site', 'hierarchical' => true, 'rewrite' => array("slug" => "product", 'with_front' => false ), 'supports' => array( 'title', 'thumbnail', 'editor', 'revisions') , 'show_in_nav_menus' => true , ) ); }
My CPT taxonomy is;
//create a custom taxonomy name it "type" for your posts function custom_taxonomy() { $labels = array( 'name' => _x( 'Product Categories', 'taxonomy general name' ), 'singular_name' => _x( 'Product Category', 'taxonomy singular name' ), 'search_items' => __( 'Search Product Categories' ), 'all_items' => __( 'All Product Categories' ), 'parent_item' => __( 'Parent Product Category' ), 'parent_item_colon' => __( 'Parent Product Category:' ), 'edit_item' => __( 'Edit Product Category' ), 'update_item' => __( 'Update Product Category' ), 'add_new_item' => __( 'Add New Product Category' ), 'new_item_name' => __( 'New Product Category Name' ), 'menu_name' => __( 'Product Categories' ), ); register_taxonomy( 'types', array('products'), array( 'hierarchical' => true, 'labels' => $labels, 'rewrite' => array( 'slug' => 'products' ), 'show_ui' => true, 'public' => true, 'show_admin_column' => true, 'query_var' => true, ) ); } // Let us create Taxonomy for Custom Post Type add_action( 'init', 'custom_taxonomy', 0 );
And my query is;
<?php $args = array( 'post_type' => 'products', 'post_status' => 'publish', 'posts_per_page' => 5, 'tax_query' => array( array( 'taxonomy' => 'types', ), ), ); $query = new WP_Query( $args ); if ( $query->have_posts() ) : while($query->have_posts()) : $query->the_post(); ?> <h3><?php echo the_title(); ?></h3> <?php endwhile; wp_reset_query(); endif; ?>
Can anyone see where I’ve gone wrong?
Thanks
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Custom posts not showing when query has custom taxonomy inside’ is closed to new replies.