I have created a custom post type called track-record (think portfolio). My code is as follows:
function track_record_register() {
$labels = array(
'name' => _x('Track Record', 'post type general name'),
'singular_name' => _x('Track Record Item', 'post type singular name'),
'add_new' => _x('Add New', 'track record item'),
'add_new_item' => __('Add New Track Record Item'),
'edit_item' => __('Edit Track Record Item'),
'new_item' => __('New Track Record Item'),
'view_item' => __('View Track Record Item'),
'search_items' => __('Search Track Record'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'has_archive' => true,
'taxonomies' => array('category'),
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'track-record' , $args );
}
This works fine and I can create a track-record item and assign it to a category. However, when I use WP_Query to list all posts in a category the custom track-record post types that are assigned to the category Track Record do not show up. If I create a post and assign it to the category Track Record in the normal fashion (by going Posts -> Add New instead of Track Record -> Add New) it works fine.
Thoughts? :/
My QP_Query() code:
$args = array('cat' => get_cat_id($category));
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<li>
<a href="<?php echo the_permalink();?>"><i class="icon-circle-arrow-right"></i></a>
<a href="<?php echo the_permalink(); ?>"><?php echo the_title(); ?></a>
</li>
<?php
endwhile;
?>
In the above code for QP_Query() the variable $category
is the name of the category which WordPress then translates in to the category’s ID. This is code is part of a widget as it is much easier for someone to remember the name of a category than it’s ID.
I just figured this out minutes after posting the question here on Stack Overflow.
For anyone that might stumble across this page with the same problem, you have to set
post_type
in the$args
for WP_Query().WP_Query() should look like this at the start: