Query posts from custom post type, and from same category as current page
-
Hello!
I’m working on a clients web site, a record label, where I’m struggling to achieve the following:I have created a custom post type for artists, and one for albums. On both of them I have enabled categories as taxonomies.
Let’s say I then add an artist called “Little David” and assign him to the Little David Category.
Then I add the album “Little David Live”, and assign the album to the Little David Category as well.
So, now we have both an artist and an album in the same category (Little David)
When on the artist page, I would like to query all albums from that artist. The problem is, I want to use a template for all artists, instead of having to make templates for each artist.
So what I’m trying to do is:
Query all posts from the custom post type “Albums”, that is placed in the same category as the current artist page. (When on the Little David artist page, list all custom post type albums from the Little David category.)
I’ve tried to combine two different examples I’ve found that are close to what I’m trying to achieve, but I’m not quite there yet.
<?php global $post; $categories = get_the_category(); $category = $categories[0]; $cat_ID = $category->cat_ID; $loop = new WP_Query( array( 'post_type' => 'albums', 'category' => '$cat_ID' ) ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> <?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?> <div class="entry-content"> <?php the_content(); ?> <?php the_post_thumbnail( 'mini-thumbnail' ); ?> </div> <?php endwhile; ?>
Any suggestions would be greatly appreciated!
- The topic ‘Query posts from custom post type, and from same category as current page’ is closed to new replies.