[wp-hackers] Custom post types - a multitude of problems

Mike Schinkel mikeschinkel at newclarity.net
Mon Jul 26 10:06:14 UTC 2010


On Jul 26, 2010, at 5:20 AM, Kris Young wrote:
> I need to output the contents (all posts, all content of those posts) of
> a specific custom category (taxonomy) and I have no idea how to get it
> done. All I want is a page that outputs all the posts for a certain
> category (and that category only) that I created using this code:
> 
> register_taxonomy("clients", array("portfolio_item"),
> array("hierarchical" => true, "label" => "Clients", "singular_label" =>
> "Client", "rewrite" => array('slug'=>'portfolio_item')));
> 
> Yes, that part was simple, but for some reason my custom taxonomy is
> completely separate from the rest of the site and irretrievable. Can't
> figure out for the life of me how to do this.

To start, I think you have your taxonomy registration wrong. (FWIW my preference would be to go with a singular for the taxonomy name, e.g. "client" but I'll leave yours as is):

  $labels = array(
    'name' => _x( 'Clients', 'taxonomy general name' ),
    'singular_name' => _x( 'Client', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Clients' ),
    'all_items' => __( 'All Clients' ),
    'parent_item' => __( 'Parent Client' ),
    'parent_item_colon' => __( 'Parent Client:' ),
    'edit_item' => __( 'Edit Client' ), 
    'update_item' => __( 'Update Client' ),
    'add_new_item' => __( 'Add New Client' ),
    'new_item_name' => __( 'New Client Name' ),
  ); 

register_taxonomy("clients", array("portfolio_item"),array(
	"hierarchical" => true, 
	"labels" => $labels, 
	"public" => true, 
	"show_ui" => true, 
	"query_var" => "clients", 
	"rewrite" => array('slug'=>'client'),
));

Many of these options will default but I went ahead and listed them all.  Read more here: 

http://codex.wordpress.org/Function_Reference/register_taxonomy

As for the rest of your question, I'm unclear.  Do you want all Portfolio Items that have *any* clients, or a specific client?  For a specific client i.e. "john-smith" I think this would be what you need (though I haven't run the code so it may have typos):

<?php
$portfolio_items = new WP_Query('post_type=portfolio_item&taxonomy=clients&term=john-smith&posts_per_page=-1');
if ( $portfolio_items->have_posts() ): 
	while ( $portfolio_items->have_posts() ) : $portfolio_items->the_post(); ?>
		<h2 id="post-<?php the_ID(); ?>" <?php post_class(); ?>><?php the_title(); ?></h2>
		<?php the_content(); 
	endwhile; 
endif; 

If you want to get all portfolio items that have *any* clients I've actually not done that before so I'll have to let someone else answer.

HTH.

-Mike


More information about the wp-hackers mailing list