I simply want to list out the books (a custom post type) organized by their respective genre (the taxonomy made with the code below). I have all the code being used listed below.
I am using the following code in a page template to call the custom post type 'book':
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$myposts = array(
'post_type' => 'book',
'posts_per_page' => '40',
'orderby' => 'id',
'order' => 'ASC',
'paged'=> $paged
);
query_posts($myposts);
?>
This is the custom post type origin in the functions.php file:
add_action( 'init', 'register_cpt_book' );
function register_cpt_book() {
$labels = array(
'name' => _x( 'Books', 'book' ),
'singular_name' => _x( 'Book', 'book' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => _x( 'Add New Book', 'book' ),
'edit_item' => _x( 'Edit Book', 'book' ),
'new_item' => _x( 'New Book', 'book' ),
'view_item' => _x( 'View Book', 'book' ),
'search_items' => _x( 'Search Books', 'book' ),
'not_found' => _x( 'No books found', 'book' ),
'not_found_in_trash' => _x( 'No books found in Trash', 'book' ),
'parent_item_colon' => _x( 'Parent Book:', 'book' ),
'menu_name' => _x( 'Books', 'book' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'A collection of books',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'custom-fields' ),
'taxonomies' => array( 'author', 'genre' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'book', $args );
}
Here is the custom taxonomy 'genres' in functions.php:
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Genres', 'genre' ),
'singular_name' => _x( 'Genre', 'genre' ),
'search_items' => __( 'Search Genres' ),
'all_items' => __( 'All Genres' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
'menu_name' => __( 'Genre' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', array( 'book' ), $args );
The code works great. Like I said, I just want to list out the books organized by their respective genre (the taxonomy made above).
So, the book loop would then look something like this:
Philosophy
Philosophy Book 1, Philosophy Book 2, Philosophy Book 3
Psychology
Psychology Book 1, Psychology Book 2, Psychology Book 3
History
History Book 1, History Book 2, History Book 3
I have tried several methods I found when googling this, but haven't been able to find anything that works. I would be greatly indebted if anyone could give me a workable answer. Thanks.
query_posts()
should be replaced bynew WP_Query()
.new WP_Query()
for each. This isn't as terrible if you use transients.. not sure I knew about transients then, but still. There is no pagination, but if this is remotely interesting, I can try to dig up the code later.