Support » Alpha/Beta/RC » 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() {
        '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:

        $new = new WP_Query('post_type=discography');
        while ($new->have_posts()) : $new->the_post();

    But if I add a category I can’t seem to filter my results as I normally would, by adding:


    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.


Viewing 15 replies - 16 through 30 (of 33 total)
  • @c3mdigital
    It looks like you are doing exactly what I have been trying to accomplish for about a week now.

    In my setup I have post-types (sermons, podcasts).
    I also have a custom taxonomy ‘series’ that is applied to both sermons and podcasts.


    You will notice on each sermon it says ‘series’ then gives a link to the series the sermon is in.

    the problem:
    If you click on one of those links, it takes you to a page that is supposed to list the sermons belonging to the ‘series’ taxonomy for the given term, followed by the podcasts doing the same thing.

    problem example:
    The first sermon ‘may 9th’ says it’s in the taxonomy series ‘general’.

    When you click general, the idea is that it takes you to a page showing all the sermons in the series taxonomy ‘general’ followed by the podcasts doing the same thing. –pretty much exactly what your site is doing–

    clicking the ‘general’ link takes you to the following page:

    The problem is all the sermons are being returned, not just the one’s in the general series.

    Here is the code I am using on the taxonomy-series.php page
    ‘post_type’ => ‘sermons’,
    ‘taxonomy’ =’series’,
    ‘term’ => $term->slug,
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => -1
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    //do stuff

    I think it’s that query that is making everything show up.

    Using your site as an example, what should my query be?

    thanks for your time on this one. It has really confused me for some time now, and I have tried every combination of queries that I can think of. None seems to work right. The theory of what I want to do is simple, but the implementation is turning out to be quite difficult.

    Actually you are not doing exactly what I am after, but it’s close.

    Instead of having a page that lists all the taxonomy terms with the related posts underneath them, I am trying to have a page that simply lists the posts belonging to a particular term. The term is determined via the slug.

    I hope that makes what I am trying to accomplish a little more concise.

    Chris Olbekson


    Level 12 Bug Squasher & Forum Moderator

    @anointed try this test to see if it’s a problem with your query:

    Rename your taxonomy-series.php to something like taxonomy-series.php.bak so it will force the page to use index.php instead which should just contain the standard <?php if (have_posts()) : while (have_posts()) : the_post(); ?> loop and see what happens. On my page I am not using a taxonomy template, I am just using index.php for that page.

    Ok, turns out it is indeed a problem with my function. It appears that I don’t need to try to insert all of my logic into the query.
    I will try rebuilding the query to only include the post-type attribute and see if that gives me what I am after. You can see that it is actually combining all of the posts, both ‘sermons & podcasts’ in the query, so it looks confusing.

    I will rebuild it to use 2 separate queries with only post-type and report back if that all works out.

    I love what you have done on your site. Looks like you have some really nice advanced functionality going on there. I would love to hear how you went about building the ‘product categories’ navigation.

    Also very interested in the modal box setup you have on individual products. To date I have only seen a very very basic tutorials on using modals wp.

    That is one fantastic site you have there.

    how to show our custom taxonomies (categories & tags) in custom post list?

    Chris Olbekson


    Level 12 Bug Squasher & Forum Moderator

    @anointed Thanks for the comments I don’t want to get to far off the original subject of this thread. I plan to write a tutorial on the model box setup in the next few days (check my profile for the link).

    Regarding the navigation, one of the great new features of 3.0 is the custom menus which allow you to add taxonomies to any of your menus using the new interface. See this screenshot:

    @od3n to show the tax’s in your posts add this in your loop somewhere:

    <?php echo get_the_term_list( $post->ID, 'classification', 'Category: ', ', ', '' ); ?>

    @c3mdigital look this screen shot.

    how to show my custom categories and tags there?

    Chris Olbekson


    Level 12 Bug Squasher & Forum Moderator

    @od3n when you register your post type add this to your $args = array(

    'taxonomies' => array( 'post_tag', 'category '),

    This was referenced from this tutorial:

    @c3mdigital but need to use my custom categories and tags, not the default one.

    Chris Olbekson


    Level 12 Bug Squasher & Forum Moderator

    You would have to create a special function to add them to the dashboard. See this post:

    ok thanks. will try and update later.

    @c3mdigital got the work done already. see here many thanks for inspired me.

    I’m using MichaelH code

    //for a given post type, return all
    $post_type = 'posts';
    $tax = 'province';
    $tax_terms = get_terms($tax);
    if ($tax_terms) {
      foreach ($tax_terms  as $tax_term) {
          'post_type' => $post_type,
          "$tax" => $tax_term->slug,
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of '.$post_type . ' where the taxonomy '. $tax . '  is '. $tax_term->name;
          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_title(); ?></a></p>

    I have 33 term slug and each of them have 4 posts. Could it be possible to break the list with pagination?

    Hey, thought I would share my limited knowledge and success story in this area! I created a custom post type called Portfolio with an associated custom taxonomy Portfolio-type. Simply put I wanted to create new “categories” for my custom post type but I called them “types”. On each single portfolio page I wanted a section at the bottom that displayed 3 related portfolio items based on the “type”. Here is the code I used, with the help of this forum and about 2 days of learning custom post types, it is beginning to make sense!

    <div class="related-posts">
     <h3 class="widget-title">Related Posts</h3>
         $query = new WP_Query(array('post_type' => 'portfolio', 'portfolio_type' => get_the_term_list( $post->ID, 'portfolio_type' )));
         while ($query->have_posts()) : $query->the_post();
         <div class="related-thumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('medium'); ?></a></div>
         <h2><a>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
         <h4><?php echo get_post_meta($post->ID, "Description", true); ?></h4>
         <div class="clear"></div>
        <?php endwhile; wp_reset_query(); ?>

    Also used MichaelH’s code mentioned by Ryan Riatno.

    Thank you soooooo much! I have been spending weeks getting this to work 🙂

Viewing 15 replies - 16 through 30 (of 33 total)
  • The topic ‘custom posts type with custom taxonomy wp_query’ is closed to new replies.