I have set up a custom post type (training) and it has the taxanomy (companies). I have assigned one post to the example taxanomy I have setup called highwaysmedia.
The following code is found on my archive-training.php page which is for this CPT. It’s displaying categories from the regular post type though, and 1 post from each. I need it to display one post from each category/taxanomy of the custom post type ‘training‘.
<?php
//for each category, show all posts
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC',
'post_type' => 'training'
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'showposts' => 1,
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>
Here is the page I have this all set up on: http://www.highwaysindustry.com/training/
I am using the TYPES plugin for creating my custom post types.
You can query for custom post type on taxonomies like this: