I have a custom post type called 'news' with a custom taxonomy called 'news category'. I'd like to create a wp query to return all 'news' posts of a certain 'news category' for example 'press'. Is there a way to write this that is universal so I don't need to make one for each 'news category'?
I think it's something similar to
$args = array(
'post_type' => 'news',
'news category' => *GET THE LABEL FROM THE PAGE SLUG SOMEHOW*
);
$query = new WP_Query( $args );
EDIT: Here is my code from my functions.php file
function my_custom_post_news() {
$labels = array(
'name' => _x( 'News', 'post type general name' ),
'singular_name' => _x( 'News', 'post type singular name' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => __( 'Add New News' ),
'edit_item' => __( 'Edit News' ),
'new_item' => __( 'New News' ),
'all_items' => __( 'All News' ),
'view_item' => __( 'View News' ),
'search_items' => __( 'Search News' ),
'not_found' => __( 'No News found' ),
'not_found_in_trash' => __( 'No News found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'News'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our news and news specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true,
);
register_post_type( 'news', $args );
}
add_action( 'init', 'my_custom_post_news' );
function mav_taxonomies_news() {
$labels = array(
'name' => _x( 'News Categories', 'taxonomy general name' ),
'singular_name' => _x( 'News Category', 'taxonomy singular name' ),
'search_items' => __( 'Search News Categories' ),
'all_items' => __( 'All News Categories' ),
'parent_item' => __( 'Parent News Category' ),
'parent_item_colon' => __( 'Parent News Category:' ),
'edit_item' => __( 'Edit News Category' ),
'update_item' => __( 'Update News Category' ),
'add_new_item' => __( 'Add New News Category' ),
'new_item_name' => __( 'New News Category' ),
'menu_name' => __( 'News Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'has_archive' => true,
);
register_taxonomy( 'news_category', 'news', $args );
register_taxonomy_for_object_type( 'news_category', 'news' );
}
add_action( 'init', 'mav_taxonomies_news', 0 );
/news
works and /news/sample-news-article
works. However,news_category/events
gives me all the news
items instead of just the event
items.
EDIT: changed 'label' to 'news category'