Custom posts type with custom taxonomy wp_query
-
I having some difficulty figuring out how to query a custom post type in a specific (custom) category..
So far I have the following code in my functions.php
function post_type_discog() { register_post_type('discography', array( 'labels' => array( 'name' => __( 'Discography' ), 'singular_name' => __( 'Discography' ), 'add_new' => __( 'Add New' ), 'add_new_item' => __( 'Add New Discography' ), 'edit' => __( 'Edit' ), 'edit_item' => __( 'Edit Discography' ), 'new_item' => __( 'New Discography' ), 'view' => __( 'View Discography' ), 'view_item' => __( 'View Discography' ), 'search_items' => __( 'Search Discographys' ), 'not_found' => __( 'No Discographys found' ), 'not_found_in_trash' => __( 'No Discographys found in Trash' ), 'parent' => __( 'Parent Discography' ), ), 'public' => true, 'show_ui' => true, 'exclude_from_search' => true, 'hierarchical' => true, 'supports' => array( 'title', 'editor', 'thumbnail' ), 'query_var' => true ) ); } add_action('init', 'post_type_discog'); add_action( 'init', 'create_discog_taxonomies', 0 ); function create_discog_taxonomies() { // Add new taxonomy, make it hierarchical (like categories) $labels = array( 'name' => _x( 'Recordings', 'taxonomy general name' ), 'singular_name' => _x( 'Recording', 'taxonomy singular name' ), 'search_items' => __( 'Search Recordings' ), 'popular_items' => __( 'Popular Recordings' ), 'all_items' => __( 'All Recordings' ), 'parent_item' => __( 'Parent Recording' ), 'parent_item_colon' => __( 'Parent Recording:' ), 'edit_item' => __( 'Edit Recording' ), 'update_item' => __( 'Update Recording' ), 'add_new_item' => __( 'Add New Recording' ), 'new_item_name' => __( 'New Recording Name' ), ); register_taxonomy('recordings',array('discography'), array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'recordings' ), )); }
Which works great (on the surface at least!). I can create a custom post type (which I can retrieve with WP_Query like so:
<?php $new = new WP_Query('post_type=discography'); while ($new->have_posts()) : $new->the_post(); the_content(); endwhile; ?>
But if I add a category I can’t seem to filter my results as I normally would, by adding:
category_name="my-category"
to the WP_Query
$new = new WP_Query('post_type=discography&category_name=my-category');
This won’t display anything! I’ve tried renaming ‘category_name’ to ‘recordings_name’, but then it just displays all the entries in that custom post type.
Interestingly enough, I have been able to filter my results by customs tags but I don’t see why I can’t get it to work with custom categories!
Help!
- The topic ‘Custom posts type with custom taxonomy wp_query’ is closed to new replies.