Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Featured posts in sidebar without duplicate posts from main loop WordPress

  • SOLVED

Hey all!

I have a problem I've been struggling with for a while now. I know how to build a featured post section above my regular wordpress loop, and then filter the already displayed posts from showing up in the main loop with all posts.

<strong>But how about the other way around?</strong>

I would like to have a featured posts section in my sidebar, showing the most recent posts from the category <em>featured</em>.

In <strong>index.php</strong> I have the normal WordPress loop:


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

// post stuff

<?php endwhile; ?>

<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>

<h2>Not Found</h2>

<?php endif; ?>


Then, in my <strong>sidebar.php</strong> I'm using this code to fetch the 5 latest featured posts:


<?php query_posts('category_name=featured&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>

// Post stuff

<?php endwhile; ?>


What I would like to do, is make some kind of a filter in the first loop, so that the second loop won't display any posts already listed in the main loop.

Also, when a visitor uses the <em>older posts link</em> to see page 2 (the next ten posts in the history of my blog), the sidebar once again shouldn't display any of the posts of that particular page (page 2), and so on...

To explain further: When the visitor is on the first page, don't show any of the posts 1-10 in the featured post section in the sidebar, when the visitor is on page two, don't show any of the posts 11-20 in the sidebar. And so on for page 3, 4, 5 etc...

Hope I'm making myself clear!

Is this possible to do, or does it only work the other way around?

Take care guys, I hope someone can help me with this!

// Jens.

Answers (3)

2011-10-07

Dylan Kuhn answers:

Maybe something like this:

<?php
global $wp_query;
$main_loop_ids = wp_list_pluck( $wp_query->posts, 'ID' );
$featured_query = new WP_Query( array(
'category_name' => 'featured',
'posts_per_page' => 5,
'post__not_in' => $main_loop_ids
) );
if ( $featured_query->have_posts() ) : while ( $featured_query->have_posts() ) : $featured_query->the_post();
?>

// post stuff

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>

<?php else : ?>

<h2>Not Found</h2>

<?php endif; ?>


Jens Filipsson comments:

That's what I want, but the total opposite.

Like this: Say that I have 20 of my best blog posts of all time in my featured post category. I want the five latest posts from this category to display in my sidebar, but only the ones not in the wordpress loop.

So i don't want to exclude them from the main wordpress loop, I want to show the five most recent ones that are <strong>not</strong> displaying in the main loop in the sidebar. I don't want to remove anything from the main loop, only from the sidebar.


Dylan Kuhn comments:

That code is meant to be the sidebar loop code. It doesn't change the main loop, just gets the list of post IDs in it.


Dylan Kuhn comments:

I see, I copied some of your index loop. This should be enough:


<?php
global $wp_query;
$main_loop_ids = wp_list_pluck( $wp_query->posts, 'ID' );
$featured_query = new WP_Query( array(
'category_name' => 'featured',
'posts_per_page' => 5,
'post__not_in' => $main_loop_ids
) );
while ( $featured_query->have_posts() ) : $featured_query->the_post();
?>

// post stuff

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>


Jens Filipsson comments:

Thank You Dylan, you are a genious! I have never seen the wp_list_pluck-function before, but that's awesome! It even works on single pages, filtering out the post I'm currently looking at! Even better!!

I'm very grateful, and will award the price money to you. Just have to figure out how I do that, since this is my first question here :)

By the way, there's no security risks by using this function in my theme?

Take care my friend!

// Jens.


Dylan Kuhn comments:

Good! As far as I know that code is in line with current best practices, should be no issues with using it all you like.

I think you click the "Vote to award prize" link under the question to finish it off. Thanks!


Jens Filipsson comments:

Sounds great Dylan, thanks again! Now I'm gonna vote! Cheers!

2011-10-07

rizaljohn answers:

Yes, it is possible!

So, only your main loop has a pagination?


Jens Filipsson comments:

Yes, only pagination on the main loop.

It´s a typical blog layout. Ten posts with excerpts with links to the single post page.

In the sidebar, I only show 5 posts and no pagination on them, it's just a list of posts i want to feature for my readers.

Cheers!

// Jens.


rizaljohn comments:

Have you tried to reset the query in the main loop? So you code looks like this:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>



// post stuff



<?php endwhile; ?>

<?php wp_reset_query(); ?>

<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>

<?php else : ?>



<h2>Not Found</h2>



<?php endif; ?>


Jens Filipsson comments:

No, but that won't make a difference. I'm looking for a filter, some sort of array or something that can save the id:s of the posts already displayed, and hide them in the featured posts section in the sidebar.


rizaljohn comments:

In your main loop, you may have like this:
<?php
global $main_loop_ids;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$limit = get_option('posts_per_page');
query_posts('showposts=' . $limit . '&paged=' . $paged);

?>

<?php if (have_posts()) : ?>< ?php while (have_posts()) : the_post(); ?>

// post stuff

<?php $main_loop_ids[] = get_the_ID(); ?>

<?php endwhile; ?>

<?php wp_reset_query(); ?>

<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>

<?php else : ?>

<h2>Not Found</h2>

<?php endif; ?>


In your sidebar.php, you can have this featured posts query:
<?php
global $main_loop_ids; // calling the variables containing all ids from main loop
?>
<?php $my_query = new WP_Query('category_name=Featured&showposts=5&order=DESC&post__not_in='. $main_loop_ids);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>

// do stuff here

<?php endwhile; ?>

2011-10-07

Luis Abarca answers:

OK, maybe using some global var

<strong>functions.php</strong>

$exclude_posts = array();



<strong>In index.php:</strong>

<?php if (have_posts()) : while (have_posts()) : the_post(); $exclude_posts[] = $post->ID; ?>
// post stuff
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h2>Not Found</h2>
<?php endif; ?>



<strong>Then, in sidebar.php :</strong>

<blockquote><?php global $exclude_posts; ?></blockquote>
<?php
$query = new WP_Query(array(
'category_name' => 'featured',
'posts_per_page' => 5,
'post__not_in' => $exclude_posts);
?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
// Post stuff
<?php endwhile; ?>
<?php unset($query); ?>


Jens Filipsson comments:

Unfortunately that didn't work, but I got the solution from Dylan. Thank's for your help!