I'm building a function that uses WP_Query to query posts from a custom post type.

I can get posts just fine, as long as I don't try to include a category parameter.

This is the code I'm using now:

 * Get albums by category
function lml_albums_by_category( ) {

    $args = array(
        'posts_per_page' => 6,
        'post_type' => albums,
        'category_name' => "new-release"
    ) ;

    $my_query = new WP_Query($args) ;

    <?php if ($my_query->have_posts()) : ?>
        <section id="">
            <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                <article class="">
                    <h3><?php the_title()  ?></h3>
                    <p><?php  the_excerpt(); ?></p>
                    <p><a href="<?php echo get_permalink()  ?>">read more</a></p>
            <?php endwhile; ?>
        </section> <!-- ENDS #...  -->
    <?php endif; ?>

<?php } ?>

If I omit the category parameter, I can get posts. But if I include it then I get no posts.

Posts of the custom type exist that have this category.

In the place of 'category_name' => "new-release" I have also tried:

'cat' => 430 // where 433 is the id of the category


'category__in' => 430

What could be going wrong here?

1 Answer 1


You can try using cat or category__in (for array of categories) instead of category-name and assign an ID to it:

$args = array(
    'posts_per_page' => 6,
    'post_type' => 'albums',
    'cat' => 4,

I also would use single quotation ' instead of double quotation " while setting a value. You are also missing quotations around your post type, which should be 'albums' instead of albums.

For further reading, check the codex page for WP_Query.

  • I probably should have added that I've tried 'cat', 'category__in'. I'll update the question to reflect this. Thanks.
    – bob
    Jun 3, 2017 at 10:00
  • 1
    @bob There is also a syntax error in your code. You should use 'albums' instead of albums. This may cause issue and omit the next lines of code.
    – Johansson
    Jun 3, 2017 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.