• 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.

    Help!

Viewing 15 replies - 1 through 15 (of 33 total)
  • what if you do this:

    $cat_id = get_cat_ID('My Category');
    $args=array(
      'cat' => $cat_id,
      'post_type' => 'discography',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $new = new WP_Query($args);

    Actually, are you trying to use your “recordings” taxonomy here?

    If so there’s this:

    <?php
    //for a given post type, return all
    $post_type = 'discography';
    $tax = 'recordings';
    $tax_terms = get_terms($tax);
    if ($tax_terms) {
      foreach ($tax_terms  as $tax_term) {
        $args=array(
          '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>
            <?php
          endwhile;
        }
        wp_reset_query();
      }
    }
    ?>
    Thread Starter Richard Sweeney

    (@theorboman)

    Wow, that’s awesome 🙂 Thank you @michaelh!

    Might I ask how one could perform a WP_Query to only display the custom posts from one custom taxonomy?

    Say in my recordings taxonomy I made 2 categories: ‘live’ and ‘studio’ and wanted to display all the discography entries in category ‘live’. How could I manage this?

    I’m sorry I lack the skills to figure it out myself and am very grateful for your help!

    Try this to query custom posts by taxonomy:

    <?php query_posts( array( 'Recordings' => 'live' ) ); ?>
    <?php if( is_tax() ) {
        global $wp_query;
        $term = $wp_query->get_queried_object();
        $title = $term->name;
    }  ?>
    
    <ul>
    <span class="tax-title"><?php echo($title); ?></span>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><?php the_excerpt() ?></li>
    
    <?php endwhile; else: ?>
    <?php endif; ?>
    </ul>
    Thread Starter Richard Sweeney

    (@theorboman)

    Thanks @c3mdigital, this returned only the name of the category, but I realized that the ‘post_type’ wasn’t mentioned at all in the query.

    I solved it with a guess!

    Just added the following to the query_posts array

    'post_type' => 'discography'

    so the whole query reads:

    <?php query_posts( array( ‘post_type’ => ‘discography’, ‘recordings’ => ‘live’ ) ); ?>

    Thanks so much for your help @c3mdigital and @michaelh – have a nice weekend 🙂

    Thread Starter Richard Sweeney

    (@theorboman)

    Just to clarify to anyone else who might be having the same problem. The only code I really needed is the following:

    <?php
      query_posts( array( 'post_type' => 'discography', 'recordings' => 'live' ) );
      if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>
    
      <h3><?php the_title(); ?></h3>
      <?php the_content(); ?>
    
    <?php endwhile; endif; wp_reset_query(); ?>

    Hey, I got a similar issue and this already helped a lot. What i am trying to do is querying recordings by id which seems to not work at all.

    query_posts("post_type=discography&recordings=2,3,4")

    neither does it when i try to with a single id or with multiple term names like

    query_posts("post_type=discography&recordings=live,studio")

    Is that possible? Would be grateful for some help 🙂

    Thread Starter Richard Sweeney

    (@theorboman)

    @kiraa – good question, I was also wondering about that one.

    I also tried to use ID’s instead of names and multiple ‘recording’ categories to filter the query results even more, but no luck. It seems we need an equivalent to

    cat=2,3

    or even better – an equivalent to

    query_posts(array('category__in' => array(2,6)));

    Would be great if one could use

    'recordings__in => array(2,3)

    All out of ideas…

    I don’t believe you can use IDs, and the ‘category__in’ construct isn’t supported either. There was talk of something like the ‘recordings__in’ but that didn’t make it in to core.

    Hm, thats a real pitty, more sophisticated queries than “get all categories” or “get only a single category” should definitley make it into the core. the whole custom taxonomy construct loses a lot of value if you cant perform more advanced queries on these entries :/

    I am building a gallery theme that uses a custom term for gallery entries, which first sounded great but if the user is not able to display a few categories at once I can pretty much dump the idea of using this. 🙁

    the above example works:

    <?php
      query_posts( array( 'post_type' => 'discography', 'recordings' => 'live' ) );
      if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>

    The following does not work:

    <?php
      query_posts( array( 'post_type' => 'discography', 'recordings' => '$term->slug' ) );
      if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>

    Why can’t I use $term->slug ?

    As I can’t use that, what can I use?

    Basically, I have a sidebar that lists all the terms for a given taxonomy. When a user clicks on a term in that list, I simply want to present a list of articles that belong to that term from a specific post type.

    I’m guessing there has to be an obvious solution to this by now….

    The following properties will be available for the $term object:

    term_id
    name
    slug
    term_group
    term_taxonomy_id
    taxonomy
    description
    parent
    count

    Here is how you would query the term slug

    if( is_tax() ) {
        global $wp_query;
        $term = $wp_query->get_queried_object();
        $slug = $term->slug;
    }
    Thread Starter Richard Sweeney

    (@theorboman)

    @c3mdigital / @annointed – do you reckon a solution is possible to query custom posts in multiple custom taxonomies for those of us lacking php skills?!

    hi theorboman,

    did your custom post show your custom taxonomy in the post list?

    i tried yours in my localhost, but doesnt show up. is it just me?

    @theorboman the quick workaround would be to set up some parent taxonomies . As an example, here is a page I created using the custom post type products that uses the parent taxonomy Liquor/Spirts which is broken down further into Tequila, Vodka etc. http://globalqualityimports.com/products/

    You might also try creating a custom loop using the [wp_query] function. Here is a very good tutorial on the subject: http://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/

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