0

I have the following custom post types: books, documents, examples, guidebooks, onlineaids. They were generated by the following code:

/*-----------------------------------------------------------------------------------*/
/* CPT - Books */
/*-----------------------------------------------------------------------------------*/
function jeo_cpt_books() {
    $labels = array(
        'name'               => _x( 'Books', 'post type general name' ),
        'singular_name'      => _x( 'Book', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'book' ),
        'add_new_item'       => __( 'Add New Book' ),
        'edit_item'          => __( 'Edit Book' ),
        'new_item'           => __( 'New Book' ),
        'all_items'          => __( 'All Books' ),
        'view_item'          => __( 'View Books' ),
        'search_items'       => __( 'Search Books' ),
        'not_found'          => __( 'No books found' ),
        'not_found_in_trash' => __( 'No books found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Books'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our books and book specific data',
        'public'        => true,
        'menu_position' => 5,
        'menu_icon'      => get_bloginfo('template_directory') . '/images/icons/icon-book.png',  // Icon Path
        'taxonomies'     => array('post_tag'), // Adds tags to cpt
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
        'has_archive'   => true,
    );
    register_post_type( 'books', $args );   
}
add_action( 'init', 'jeo_cpt_books' );

/* CPT - Books - Taxonomies */

function jeo_cpt_book_tax() {
    $labels = array(
        'name'              => _x( 'Book Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Book Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Book Categories' ),
        'all_items'         => __( 'All Book Categories' ),
        'parent_item'       => __( 'Parent Book Category' ),
        'parent_item_colon' => __( 'Parent Book Category:' ),
        'edit_item'         => __( 'Edit Book Category' ), 
        'update_item'       => __( 'Update Book Category' ),
        'add_new_item'      => __( 'Add New Book Category' ),
        'new_item_name'     => __( 'New Book Category' ),
        'menu_name'         => __( 'Book Categories' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( 'book_category', 'books', $args );
}
add_action( 'init', 'jeo_cpt_book_tax', 0 );

(and so on)...

I am trying to loop through each CPT on a page-template for a different page and display them per "category" (which is a taxonomy). The following is the code I'm trying to use, with not much luck:

<?php $args = array(
'post_type'     =>  'books',
'book_category'     =>  get_the_term_list( $post->ID, 'guidebook_category' ),
'posts_per_page'    =>  -1,
'order'             =>  'ASC',
'orderby'           =>  'ID'
);
$the_query = new WP_Query( $args );           
if ( $the_query->have_posts() ) : ?>
    <h2>Books:</h2>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <div class="resource_masterindex">
            <?php the_excerpt(); ?>
        </div>
        <div class="clear"></div>
    <?php endwhile; endif; ?>
    <a href="#top" class="backtotop">Back to the top</a>
    <?php wp_reset_postdata(); ?>

The above code seems to grab everything from the right CPT, but I want display ONLY the posts that are in the category called "Step2: Prepare" or "Step3: Find". Help is greatly appreciated.

1 Answer 1

0

I believe you need a tax_query like the following from the Codex:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'people',
            'field' => 'slug',
            'terms' => 'bob'
        )
    )
);
$query = new WP_Query( $args );

In your case that would look something like:

$args = array(
  'post_type'     =>  'books',
  'tax_query' => array(
     array(
       'taxonomy' => 'book_category',
       'field' => 'slug',
       'terms' => array('step2-prepare','step3-find')
     )
  ),
  'posts_per_page'    =>  -1,
  'order'             =>  'ASC',
  'orderby'           =>  'ID'
);
$the_query = new WP_Query( $args );

I am guessing at the taxonomy slugs--'step2-prepare','step3-find' -- but I think the syntax is correct. Untested as I don't have your taxonomy and posts in my database.

That should "ONLY the posts that are in the category called "Step2: Prepare" or "Step3: Find"."

You can't ORDER BY a category/taxonomy-- Using wp_query is it possible to orderby taxonomy? What you have to do is Loop over the posts, create a new sorted array, then Loop over that to display the posts. Doing so could make pagination a bit weird, so be careful. This is very rough and without your taxonomy and posts it is hard to test anything but...

$sorted = array();
foreach ($the_query as $tq) {
  $terms = wp_get_post_terms( $tq->ID, 'book_category');
  $sorted[$terms[0]->slug] = $tq;
}

However, there are problems with that. The first that comes to mind is that if you have multiple terms you have to pick one of them or insert your post multiple times in the $sorted array.

4
  • Thanks! That seems to work for the first step. Maybe my initial question didn't get completely explained. Adding the tax_query array seems to filter down to the "category" I want. I am using one page-template for several pages. I have 7 of these "steps" (categories) and the one page-template is applied to the whole thing. I have a body_tag ID set to the page slug, thus step1-X and step2-Y and step3-Z etc. Would there be some kind of filter or if-else statement to add to loop thru each CPT, and order them by categories? Mar 17, 2013 at 20:52
  • I managed to accomplish what I needed, however, I am almost positive that there is a much simpler method. Here is my complete code -- is there any way to trim it down? --- pastebin.com/i4Ud3BRs Mar 17, 2013 at 22:36
  • Hmm... I didn't see your edited post with the additional info until just now, after I have added all these WP_queries (see pastebin from above comment). If there's no real adverse problems to the way I have it set up, I am ok with that, because it works. Mar 17, 2013 at 22:38
  • You have multiple queries which could cause trouble in high traffic. Otherwise, I didn't notice any real issues.
    – s_ha_dum
    Mar 17, 2013 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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