i have a filter on my template which returns posts from a given category and displays them in a slider gallery. This is the code:

 if (is_page()) {
     $cat=get_cat_ID($post->post_title); //use page title to get a category ID
     $posts = get_posts( array('category' => 16) );
     if ($posts) {
         foreach ($posts as $post):
             setup_postdata($post); ?>

This works fine with "normal" posts. But I have created 3 custom post types (some of the entries tagged with the same category). And they don't show up. Could anyone please help how to customize this code in order to be able to use it also with CPT's? Thank you, Ralf


It sounds like all you need to do is specify the 'post_type' argument which is only 'post' by default.

$posts = get_posts( array(
    'category' => 16,
    'post_type' => array( 'post', '{another_post_type}', '{another_post_type}', '{another_post_type}' )
) );

You might also consider using WP_Query to make a custom loop instead. That's a bit personal preference but is probably the best practice for what it looks like you're doing.

  • Your first solution works perfectly! Thank you very much! – Ralf Glaser Aug 22 '14 at 18:08
  • 1
    Great. Make sure to accept the answer so others know! – mrwweb Aug 22 '14 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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