1

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:

<?php
 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

1
  • +1 for not using query_posts
    – Tom J Nowell
    Commented Aug 22, 2014 at 18:08

1 Answer 1

1

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.

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

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.