Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to have to query loops running on my blog homepage:

  1. Featured posts on top displaying 3 posts from the category : "Featured"
  2. Underneath it just the basic loop of wordpress which display the latest (10 or other, whatever is selected within the admin) with "next" and "previous" buttons for archive posts (paged)

Important: The top part will display only 3 posts of the "Featured" category but the regular blog loop underneath will display ALL the posts including the "Featured" ones (even if it's a duplication - the featured posts have to appear also in the regular blog loop)

it seems prety easy but when I tried it couple of times even using rewind_posts it broke the pagination.

This is the code I originally used:

// Featured posts section

<section class="featured">
<div class="title">
<h4>Featured</h4>
</div>

            <?php query_posts('showposts=3&category_name=featured'); ?>
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article>
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('post-featured'); ?></a>
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php excerpt('20'); ?></p>
<div class="more"><a href="<?php the_permalink() ?>">Continue Reading</a></div>
</article>
            <?php endwhile; else: ?>
 <p>Sorry, no posts matched your criteria.</p>
 <?php endif; ?>
 <?php wp_reset_query(); ?>
</section>


// Regular normal posts section

<section class="home-posts">
  <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

  <article class="post-details">
    <div class="header">
      <h3 class="cat">
        <?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
      </h3>
    </div>
    <div class="right-image">
      <?php the_post_thumbnail('post-thumb'); ?>
    </div>
    <div class="title">
      <h2><a href="<?php the_permalink() ?>">
        <?php the_title(); ?>
        </a></h2>
    </div>
    <div class="date">
      <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
    </div>
    <div class="entry">
      <p>
        <?php excerpt('50'); ?>
        ...</p>
      <?php include(TEMPLATEPATH . '/inc/share.php'); ?>
      <a href="<?php the_permalink() ?>" class="more">Continue Reading >></a> </div>
  </article>
  <?php endwhile; ?>
  <?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
  <?php else : ?>
  <h2>&nbsp;</h2>
  <?php endif; ?>
</section>

hope someone can help Thanks

share|improve this question
1  
"but the regular blog loop underneath will display ALL the posts including the "Featured" ones" I don't understand this part. The posts from the featured category will show automatically in the second (main) loop according to the pagination/sort order. Meaning, if you are sorting by date (newest first) and showing 10 posts per page, if you have 10 posts more recent than your most recent featured post, you won't see the featured post on page 1. It will be on page 2. –  helgatheviking Mar 18 '13 at 17:14
    
First of all - YES, I do want to show the "Featured" posts again in the main loop even if they appeared in the "Featured" section and secondly - I tried looking at page-2, page-3 etc etc and couldn't find the featured posts there.. –  gil hamer Mar 18 '13 at 18:14
    
I thought that's what you wanted, so we're clear there. But I am confused why they aren't appearing in the loop. They aren't some other kind of post type? This code you are showing is your index.php? Can you deactivate all your plugins? Do you have anything changing the sort order? –  helgatheviking Mar 18 '13 at 21:04

1 Answer 1

up vote 0 down vote accepted

You can use this code to get 3 posts from category Featured

$args = array(
  'category_name' => 'featured',
  'posts_per_page' => 3
);

$featured_posts = new WP_Query( $args );

if ( $featured_posts->have_posts() ):
  while ( $featured_posts->have_posts() ):
    $featured_posts->the_post();

    // Here you can use the normal loop functions like 'the_title()' to display your
    // 3 posts from the category 'Featured'
  endwhile;
endif;

After this code, it's important you place the following line:

wp_reset_postdata();

After looping through a separate query, this function restores the $post global to the current post in the main query.

Edit
You can use the following code to get a loop with pagination enabled:

$paged = ( get_query_var( 'paged' ) )? absint( get_query_var( 'paged' ) ): 1;

$args = array(
  'posts_per_page' => get_option( 'posts_per_page' ),
  'paged' => $paged
);

$posts = new WP_Query( $args );

// Merge the two results
$posts->posts = array_merge( $posts->posts, $featured_posts->posts );
// Increase the post_count to display all the items in the merged posts array
$posts->post_count += $featured_posts->post_count;

if ( $posts->have_posts() ):
  while ( $posts->have_posts() ):
    $posts->the_post();

    // Here you can use the normal loop functions like 'the_title()' to display your
    // posts from the category 'Featured'
  endwhile;
endif;
share|improve this answer
    
Tried it but it is not displaying the "Featured" posts again in my regular loop with the pagination.. that's what I am trying to achieve: I have to show the "Featured" posts again in the regular loop. is it possible using that code? Thanks –  gil hamer Mar 18 '13 at 14:15
    
Ah OK! I'll add some more code to my answer for your second loop –  Mike Madern Mar 18 '13 at 14:25
    
Almost.. it is now showing the "Featured Posts" on top and also the regular loop shows only the "Featured Posts" I need the regular loop to show all blog posts including the "Featured Posts" (so they show on top and in the blog) I appreciate your efforts! –  gil hamer Mar 18 '13 at 14:42
    
I think it has something to do with 'category_name' => 'featured',? –  gil hamer Mar 18 '13 at 14:43
    
The 'category_name' => 'featured' is the reason you only get "Featured" posts. But if you want all posts including the ones that are in category featured, the normal loop (without 'category_name' => 'featured') will be sufficient right? Or do you want to get the last 10 items of posts AND the featured posts as duplicate in it? –  Mike Madern Mar 18 '13 at 14:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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