WordPress.org

Forums

A LOOP with multiple custom post types and $query_string (3 posts)

  1. hilj
    Member
    Posted 3 years ago #

    Loop like this...

    global $query_string;
        $posts = query_posts($query_string . 'posts_per_page=3&post_type=music');
        if (have_posts()) : while (have_posts()) : the_post();

    ...works great! Keeps the original query around and pagination works. But it has all the post types in it. I have 4 custom post types. I would like to exclude the pages.

    This loop display the custom post types and posts, but not pages. But...

    query_posts(array('posts_per_page' => 3, 'post_type' => array('recommendations', 'posts', 'photos', 'food', 'music')));
        if (have_posts()) : while (have_posts()) : the_post();

    ...the $query_string won't work when using the array method. Like it says here

    If you don't need to use the $query_string variable, another method exists that is more clear and readable, in some more complex cases. This method puts the parameters into an array.

    Thanks for all the help :)

  2. Big Bagel
    Member
    Posted 3 years ago #

    If you're just looking for pagination to work and don't need the entire query string:

    query_posts( array( 'posts_per_page' => 3, 'post_type' => array( 'recommendations', 'posts', 'photos', 'food', 'music' ), 'paged' => get_query_var( 'paged' ) ) );
    
    if ( have_posts() ) : while( have_posts() ) : the_post();

    If it doesn't work, you can also try the query variable "page" instead; it can depends on where the query is located.

    'paged' => get_query_var( 'page' )

    If you need the query string preserved, you can try this. I've never tested it, so I'm not entirely sure that it works though.

    global $query_string;
    $posts = query_posts( $query_string . '&posts_per_page=3&post_type=recommendations,posts,photos,food,music' );
    
    if ( have_posts() ) : while( have_posts() ) : the_post();
  3. hilj
    Member
    Posted 3 years ago #

    Actually I don't need the original query, it just enabled the pagination to work. The first loop you posted does just that and works great, thanks!!!

    The post_type needs to be in array form. This wont work.

    query_posts( $query_string . 'post_type=recommendations,posts,photos,food,music' );

Topic Closed

This topic has been closed to new replies.

About this Topic