0

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/eventsgives me all the news items instead of just the event items.

EDIT: changed 'label' to 'news category'

  • 1
    If your custom post type has_archive => true, Wordpress should do this by default. If you go to: "*your_url*/label/events" does it show all your events? Or are you after something else? – ngearing Jul 12 '16 at 3:16
1

Good news - there's nothing you need to do. WordPress does this for you out of the box, providing you have set up your post type and taxonomy to do so.

You should be able to access your posts at http://example.com/labels/events, once you've checked:

  • that your post type is publicly_queryable (either the public argument or publicly_queryable argument is set to true by your register_post_type() call),
  • your taxonomy slug isn't set to something different (in the rewrite -> slug argument of your register_taxonomy() call), and
  • your permalinks have been flushed since the post type and taxonomy were registered (to flush permalinks, just visit Settings -> Permalinks and click Save).

There's a few other things you may want to look into as well, including:

  • the rewrite -> with_front argument sent to register_taxonomy(), which defines whether your default permalink base should be included (eg. some people use '/blog' before all permalinks),
  • whether you, or any plugins you're running, have any other custom rewrites set up (i.e. calls to add_rewrite_rule()) - these might change the default behaviour, and
  • whether the strongly recommended call to register_taxonomy_for_object_type() has been made after the call to register_taxonomy(),
  • and whether all of this is happening within the init action.

If this isn't working for you, there may be another argument that is changing the default behaviour; in which case you could always edit your question to add your post type & taxonomy registration code.

  • Thank you for your answer. I have my code mostly up and working. I updated my question with more details. Can you take a look if you have a moment? – Austin Jul 12 '16 at 15:13
  • Hey @Austin, I just grabbed your code, stuck it in my test install and created a few posts and terms and it's working fine; in the term archive I'm only getting the posts that I specifically assigned to that term. Do you have anything else perhaps overriding the query? Also, did you flush permalinks? – Tim Malone Jul 13 '16 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.