I have searched all over StackOverflow for this, but no solution...
I'm attempting to display all posts from a specific category that is assigned to a post_type I have created. The list of posts (consisting of their title, description, image, etc) of such category will be displayed into the specific category's template page.
To clarify...I have a post_type called "mch_room". I created 3 categories for it, which are Living Room, Bedroom, and Bathroom. Each of these categories has their own template page. Inside each of these categories pages, I would like to display a roll of all posts associated with that category ONLY. So category-31.php (which is the category template page for Living Room), will showcase all posts assigned to the "Living Room" category, displaying each post's text, featured image, etc.
I have gotten pretty close to getting this to work, but instead of all the posts being showcased from a SPECIFIC category, all posts from all categories are being displayed; even after declaring a specific category.
Here's my code:
Code for Category-31.php, which is the template page for the category "Living Room"
<?php
$args = array(
'post_type' => 'mch_room',
'categories'=> 'living-room');
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_field('room_name'); ?></a></p>
<p><?php the_field('room_description'); ?></p><?php
endwhile;
}
wp_reset_query();
?>
Code for function.php, which is where I have registered the post_type "mch_room" and the taxomony "category"
add_action('init','add_categories_to_cpt');
function add_categories_to_cpt(){
register_taxonomy_for_object_type('category', 'mch_room');
}
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'mch_room',
array(
'labels' => array(
'name' => __( 'Rooms' ),
'singular_name' => __( 'Room' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'rooms'),
'hierarchical' => false,
'menu_position' => 2,
'taxonomies' => array( 'post_tag', "category"),
)
);
}
I don't get it..What am I doing wrong? Why are all posts throughout "mch_room" are being showcased, when I'm specifically asking for posts from only the category "Living Room" to be showcased?
Is there anything I can do? I would greatly appreciate a response, Thanks.
If "living-room" is slug of your category, you should use 'category_name'=> 'living-room'. "categories" is wrong parameters.