WordPress.org

Ready to get started?Download WordPress

Forums

custom posts type with custom taxonomy wp_query (34 posts)

  1. Richard Sweeney
    Member
    Posted 4 years ago #

    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!

  2. MichaelH
    Member
    Posted 4 years ago #

    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);
  3. MichaelH
    Member
    Posted 4 years ago #

    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();
      }
    }
    ?>
  4. Richard Sweeney
    Member
    Posted 4 years ago #

    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!

  5. 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>
  6. Richard Sweeney
    Member
    Posted 4 years ago #

    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 :-)

  7. Richard Sweeney
    Member
    Posted 4 years ago #

    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(); ?>
  8. Kiraa
    Member
    Posted 4 years ago #

    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 :)

  9. Richard Sweeney
    Member
    Posted 4 years ago #

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

  10. MichaelH
    Member
    Posted 4 years ago #

    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.

  11. Kiraa
    Member
    Posted 4 years ago #

    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. :(

  12. Anointed
    Member
    Posted 4 years ago #

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

  13. 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;
    }
  14. Richard Sweeney
    Member
    Posted 4 years ago #

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

  15. Od3n
    Member
    Posted 4 years ago #

    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?

  16. @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/

  17. Anointed
    Member
    Posted 4 years ago #

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

    example:
    http://deardaddy.org/sermons/

    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:
    http://deardaddy.org/series/general/

    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
    `<?php
    $args=array(
    '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
    <?php
    endwhile;
    }
    wp_reset_query();
    ?>

    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.

  18. Anointed
    Member
    Posted 4 years ago #

    @c3mdigital
    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.

  19. @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 http://deardaddy.org/series/general/ 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 http://globalqualityimports.com/classification/liquor-spirits/ page I am not using a taxonomy template, I am just using index.php for that page.

  20. Anointed
    Member
    Posted 4 years ago #

    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.

    http://deardaddy.org/series/general/page/2/
    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.

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

  21. Od3n
    Member
    Posted 4 years ago #

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

  22. @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: http://farm5.static.flickr.com/4053/4675727759_0683e08eb9.jpg

    @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: ', ', ', '' ); ?>

  23. Od3n
    Member
    Posted 4 years ago #

    @c3mdigital look this screen shot. http://dl.dropbox.com/u/2317543/Screen%20shot%202010-06-07%20at%202.54.14%20PM.png

    how to show my custom categories and tags there?

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

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

    This was referenced from this tutorial: http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress

  25. Od3n
    Member
    Posted 4 years ago #

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

  26. You would have to create a special function to add them to the dashboard. See this post: http://www.heinencreative.com/archives/tutorials/creating-custom-post-types-in-wordpress-3-0/

  27. Od3n
    Member
    Posted 4 years ago #

    ok thanks. will try and update later.

  28. Od3n
    Member
    Posted 4 years ago #

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

  29. Ryan Riatno
    Member
    Posted 4 years ago #

    I'm using MichaelH code

    <?php
    //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) {
        $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();
      }
    }
    ?>

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

  30. jhopson
    Member
    Posted 4 years ago #

    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>
    
     <ul>
         <?php
         $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();
         ?>
    
      <li>
         <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>
      </li>
    
        <?php endwhile; wp_reset_query(); ?>
     </ul>
    
    </div>

    http://focalpoint.themedemo.net/portfolio/waterfalls/

Topic Closed

This topic has been closed to new replies.

About this Topic