• Resolved rjpinney

    (@rjpinney)


    Hello,

    I’m aware there a few other threads on this topic but I’m afraid I can’t make any sense of them, so apologies for the duplication.

    I have three custom post types running, in addition to ‘posts’. I want to run a loop that pulls all posts categorised under a particular category (bulletin), but I can only get it query a single post type.

    This is the code I have, which works for one, but I’ve not managed to successfully adapt it to query more than one post type.

    <?php query_posts('post_type=post&cat=3&showposts=5'); ?>
    		<?php while ( have_posts() ) : the_post(); ?>
    
    		<div class="post_type">
    		<?php if ( 'post' == get_post_type()  ) : ?>FEATURE<?php endif; ?>
    		<?php if ( 'report' == get_post_type()  ) : ?>REPORT<?php endif; ?>
    		<?php if ( 'opinion' == get_post_type()  ) : ?>OPINION<?php endif; ?>
    		<?php if ( 'bookmark' == get_post_type()  ) : ?>BOOKMARK<?php endif; ?>
    		</div>
    
    		<a href="<?php the_permalink(); ?> "><?php the_title(); ?></a>
    
    		<div class="excerpt">
    		<?php echo get_the_excerpt(); ?>
    		</div>
    
    		<?php endwhile; ?>

    Any help would be much appreciated!

    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Just change the query_posts parameters a bit….

    query_posts( array(
         'post_type' => array( 'post', 'report', 'opinion', bookmark' ),
         'cat' => 3,
         'showposts' => 5 )
         );

    That should take care of it for you.

    Thread Starter rjpinney

    (@rjpinney)

    Thanks wspencer, I’ve tried changing at as you suggest but I’m afraid it just crashes the page – no doubt because I haven’t put it in quite right. This is what I now have, incorporating the code you provided above:

    <?php query_posts( array(
         'post_type' => array( 'post', 'report', 'opinion', bookmark' ),
         'cat' => 3,
         'showposts' => 5 )
         ); ?>
    		<?php while ( have_posts() ) : the_post(); ?>
    
    		<div class="post_type">
    		<?php if ( 'post' == get_post_type()  ) : ?>FEATURE<?php endif; ?>
    		<?php if ( 'report' == get_post_type()  ) : ?>REPORT<?php endif; ?>
    		<?php if ( 'opinion' == get_post_type()  ) : ?>OPINION<?php endif; ?>
    		<?php if ( 'bookmark' == get_post_type()  ) : ?>BOOKMARK<?php endif; ?>
    		</div>
    
    		<a href="<?php the_permalink(); ?> "><?php the_title(); ?></a>
    
    		<div class="excerpt">
    		<?php echo get_the_excerpt(); ?>
    		</div>
    
    		<?php endwhile; ?>
    Thread Starter rjpinney

    (@rjpinney)

    Does anyone have any suggestions for this? I’m still stuck. Thanks!

    Looks like you left out a ‘ in your query in front of bookmark

    Thread Starter rjpinney

    (@rjpinney)

    Very well spotted, that was precisely the problem!

    Thanks!

    Thanks for pointing me in the right direction. I just went through figuring this out, and ended up doing this instead: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    WordPress recommends it over query_posts() for loading time, and it definitely worked better for me. I’m using Infinite Scroll, which was having problems getting the posts when I used query_posts(), but works perfectly when I filter it as they describe with pre_get_posts(). Plus, this is a little cleaner since it’s off in the functions.php file (or even in an included additional functions file!) so you don’t have to clutter up your front end files.

    Sorry, that was misleading, “pre_get_posts” is not a function. Here’s the format:

    <?php add_action( 'pre_get_posts', 'your_function_name' ); ?>

    your_function_name() then goes to define which posts/post types are queried where, etc.

    I’d like to say again that this method worked great for me in combining the custom posts and normal posts into the same query.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Query multiple custom post types in single loop’ is closed to new replies.