WordPress.org

Forums

[resolved] Query multiple custom post types in single loop (8 posts)

  1. rjpinney
    Member
    Posted 2 years ago #

    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

  2. wspencer
    Member
    Posted 2 years ago #

    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.

  3. rjpinney
    Member
    Posted 2 years ago #

    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; ?>
  4. rjpinney
    Member
    Posted 2 years ago #

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

  5. jcrea
    Member
    Posted 2 years ago #

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

  6. rjpinney
    Member
    Posted 2 years ago #

    Very well spotted, that was precisely the problem!

    Thanks!

  7. benarobinson
    Member
    Posted 1 year ago #

    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.

  8. benarobinson
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic