[resolved] Query posts from custom post type, and from same category as current page (5 posts)

  1. jenseo
    Posted 5 years ago #

    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.

    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' ); ?>
    <?php endwhile; ?>

    Any suggestions would be greatly appreciated!

  2. jenseo
    Posted 5 years ago #

    I still haven't found a solution for this, is it possible to do? Or can someone give me a better way to achieve this perhaps?
    Thank you!

  3. gnirsh
    Posted 5 years ago #

    I am having the SAME EXACT problem. If you sort this out on your own, please post back in here. I will do the same.

    Looking over your code the only thing I noticed is that it seems to me that this line:
    $loop = new WP_Query( array( 'post_type' => 'albums', 'category' => '$cat_ID' ) ); ?>

    should be replaced with this:
    $loop = new WP_Query( array( 'post_type' => 'albums', 'cat' => '$cat_ID' ) ); ?>

    Here is how I'm doing mine currently, the only difference is in the fact that I leave the initial variable as is.

    <?php $category = get_the_category(); ?>
    <?php $loop = new WP_Query( array( 'post_type' => 'calls_to_action', 'cat' => '$category[0]->cat_ID' ) ); ?>

    I've echoed the category variables after the get, so I know that part of it is working. And I echoed them again after setting the new query, so I know the query didn't somehow reset the variable. I've double checked that the relevant posts are in fact associated with their proper categories, and they are.

    I'm not sure what else to do, I've tried a lot of variations and can't get anything to work.

  4. gnirsh
    Posted 5 years ago #

    wow, I figured it out. So simple.

    remove the quotes around the variable in the query. as such:

    $loop = new WP_Query( array( 'post_type' => 'albums', 'cat' => $cat_ID ) ); ?>

    Works fine for me now. And I only wasted like a whole day on this! :)

  5. jenseo
    Posted 5 years ago #

    Thanks a lot gnirsh, now it works for me as well!
    I guess the solution was so simple that it was hard to find! :)

Topic Closed

This topic has been closed to new replies.

About this Topic