I’d like the first three loops to each show one post. Then, I’d like the fourth loop to show the remaining posts left in the main query (if admin read is set to 10, then loop four should show 7 posts) – avoiding duplication of the 3 posts above.
For some strange reason, the do_not_duplicate IDs are not being stored in the second and third loop arrays – so the loop 1 is showing 1 post, and loop 4 is showing 9 posts. Essentially, loops 2 and 3 are being completely ignored.
I cannot for the life of me figure out why. Can someone please help? Thank you!
Note: Here is the full code in pastebin: https://pastebin.com/57KCEpxK
<?php get_header(); ?>
<h1>Loop 1</h1>
<?php $do_not_duplicate = array();
$my_query = new WP_Query(array('posts_per_page' => 1, 'post__not_in' => get_option( 'sticky_posts' ) ) );
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID; ?>
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<h1>Loop 2</h1>
<?php
$args=array(
'posts_per_page' => 1,
'post__not_in' => $do_not_duplicate,
'category__in' => array($category->term_id)
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID; ?>
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<h1>Loop 3</h1>
<?php $args=array(
'posts_per_page' => 1,
'post__not_in' => array_merge($do_not_duplicate, get_option( 'sticky_posts' )),
'category__in' => array($category->term_id)
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID; ?>
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<h1>Loop 4 (leftovers)</h1>
<?php if (have_posts()) : while (have_posts()) : the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
?>
<h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
<?php get_footer(); ?>
[mod note – please do not bump, it is not permitted on these forums]
]]>Here is my code:
<?php $my_query = new WP_Query('category_name=inspiration&posts_per_page=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
<li><a href="<?php echo get_permalink(); ?>">
<?php the_title( '<h2>', '</h2>' ); ?></a> <?php the_excerpt(); ?></li>
<?php endwhile; ?>
<!-- Do other stuff... -->
<?php if (have_posts()) : while (have_posts()) : the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
echo '<li><h2><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </h2></li> ';
}
?>
<?php endwhile; endif; ?>
And here is a link to the site (the code is in the sidebar.php):
https://www.dernet.co.uk/TempAYL
thanks!
]]>I have four loops in index.php
1st: featured category posts carousel (which is already working way before)
2nd: Certain cat post, 1 post_per_page, with additional class in most parent div
3rd: Certain cat posts, 2 post_per_page, with additional class in most parent div
4th: The rest of the posts, with additional class in most parent divs, hould do_not_duplicate posts from above category except for featured
then I want the 4th loop to be paginated — like page/2/ will show the next page of the rest of the posts (aka recent posts) in the 4th loop.
Here is my code
<!-- start content -->
<div id="content">
<?php if(is_home() && !is_paged()): ?>
<?php // Featured posts go here.
include (TEMPLATEPATH . "/featured.php"); ?>
<?php endif;?>
<?php if(is_home() && !is_paged()): ?>
<div id="highlightcat1">
<h3 class="head">Latest Projects <a title="More Projects Updates" href="https://ygladies.com/category/ygladies/projects">more+</a></h3>
<?php if( $themesbyrozeh_settings['highlighted_cat1'] ) : ?>
<?php query_posts('posts_per_page=1&cat=' . $themesbyrozeh_settings['highlighted_cat1'] ); ?>
<?php while (have_posts()) : the_post(); ?>
<?php $do_not_duplicate[] = $post->ID; ?>
<div class="post ptop red">
<div class="feat">
<?php
// Must be inside a loop.
if ( has_post_thumbnail() ) {
the_post_thumbnail('ptop');
}
else {
// nothing here
}
?>
<span class="date"><?php the_time('d F y') ?></span>
</div>
<div class="entry">
<div class="entryinfo">
<?php edit_post_link('Edit', '<span class="edit">', '</span> -'); ?> <span class="author"><?php the_author() ?></span> - <span class="sdate"><?php the_time('d/m/y') ?></span> - <span class="comment_count"><?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></span>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>
<p><?php echo limit_words(get_the_excerpt(), '30'); ?>.</p>
<a class="readmore" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">Read More →</a>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
<?php endif;?>
<?php if(is_home() && !is_paged()): ?>
<div id="highlightcat2">
<h3 class="head">Latest SNS Updates <a title="More SNS Updates" href="https://ygladies.com/category/snsupdates">more+</a></h3>
<?php if( $themesbyrozeh_settings['highlighted_cat2'] ) : ?>
<?php query_posts('posts_per_page=2&cat=' . $themesbyrozeh_settings['highlighted_cat2'] ); ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$extra_classes = array('orange','green','blue','indigo','violet','pink','red');
$extra_class = $extra_classes[$wp_query->current_post%count($extra_classes)];
?>
<?php $do_not_duplicate[] = $post->ID; ?>
<div class="post ptop2 <?php echo $extra_class; ?> clearfix">
<div class="feat">
<?php
// Must be inside a loop.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
// nothing here
}
?>
<span class="date"><?php the_time('d F y') ?></span>
</div>
<div class="entry">
<div class="entryinfo">
<?php edit_post_link('Edit', '<span class="edit">', '</span> -'); ?> <span class="author"><?php the_author() ?></span> - <span class="comment_count"><?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></span>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>
<p><?php echo limit_words(get_the_excerpt(), '30'); ?>.</p>
<a class="readmore" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">Read More →</a>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
<?php endif;?>
<h3 class="head">Recent Posts</h3>
<div id="entries" class="clearfix">
<?php query_posts(array('post__not_in'=>$do_not_duplicate));
if (have_posts()) : while (have_posts()) : the_post();
?>
<?php
$extra_classes = array('blue','indigo','violet','pink','red','orange','green');
$extra_class = $extra_classes[$wp_query->current_post%count($extra_classes)];
?>
<!-- start entry post -->
<div class="post pfloat <?php echo $extra_class; ?>" id="post-<?php the_ID(); ?>">
<div class="entry">
<div class="feat">
<?php
// Must be inside a loop.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
// nothing here
}
?>
<span class="date"><?php the_time('d F y') ?></span>
</div>
<div class="entryinfo">
<?php edit_post_link('Edit', '<span class="edit">', '</span> -'); ?> <span class="author"><?php the_author() ?></span> - <span class="comment_count"><?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></span>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div>
<p><?php echo limit_words(get_the_excerpt(), '30'); ?>.</p>
</div>
</div>
<!-- end entry post -->
<?php endwhile; ?>
<?php else : ?>
<div class="post">
<div class="entry">
<h2>Not Found</h2>
Sorry, but you are looking for something that isn't here.
</div>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
<div id="pagi">
<?php previous_posts_link('← Previous') ?><?php rozehtheme_pagination(); ?><?php next_posts_link('Next →') ?>
</div>
</div>
<!-- end content -->
I thought I made it work (the classes are there and there are no duplicate posts but when I went to page/2/, it’s displaying the same posts in loop 4.
I hope anyone can help me here. I will forever be grateful.
the posts are all in a single category, with no tags etc. it’s basically just a list of posts relating to events on certain dates.
i’m using wp_query to list posts based on posts_status=future.
however many of these posts will span many days or weekends, each will have it’s own booking. I have created a widget to display a list of these events on certain pages.
however the client wants to know if i can remove the duplicate items.
i looked everywhere, but do_not_duplicate works on post->ID, not useful in my situation as all the posts are in the same category and will have seperate id’s. though the titles for multiple events held weekly are identical.. is there a way i can change or add functionality so that i could possibly use $do_not_duplicate = $post->TITLE instead?
i don’t mind delving into the core if i know what to look for, but i wondered if there was an easier way of achieving this?
]]>In functions.php:
global $do_not_duplicate;
$do_not_duplicate = array();
function latest_template($category){
if (is_front_page() && !is_paged()){
$temp_query = $wp_query;
$my_query = new WP_Query('category_name='.$category.'&posts_per_page=4');
// Start of 'The Loop'
if(have_posts()){
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID;
[etc., to the end of the latest_template function]
In the post loop template:
global $do_not_duplicate;
// Start of 'The Loop'
if (is_front_page()){
query_posts(array('post__not_in'=>$do_not_duplicate));
}
if(have_posts()){
while (have_posts()) : the_post();
All the posts that are getting pulled in by the function in functions.php are also appearing in the loop, even though I have attempted to create a separate query for the front page that will exclude them.
I’m sure the problem is very elementary, but I’m not very strong in PHP and globals are a little over my head (apparently), so I haven’t been able to spot the problem. Can anyone point me in the right direction?
Thank you!
]]>Here’s an example:
https://billyshebar.com/wp/dark-matter/
Notice that “Dark Matter (2007)” is the post being viewed, yet it also appears in the sidebar.
Thanks!
Code for single “portfolio” post:
Code for sidebar-portfolio.php:
[dito]
]]>I am aware that I have to declare a global variable and assign post IDs to that variable in an array, but I am not doing it right, apparently.
Here is the code for the feature slider loop:
global $themename_do_not_duplicate;
$themename_do_not_duplicate = array();
$arr = array();
$i=1;
$width = $instance['ImgWidth'];
$height = $instance['ImgHeight'];
$cat_posts = new WP_Query("showposts=" . $instance["featuredNum"] . "&cat=" . $instance["cat"]);
while ($cat_posts->have_posts()) : $cat_posts->the_post() $themename_do_not_duplicate[] = $posts->ID;?>
<li><a href="<?php the_permalink();?>">
<?php
$post_title = get_the_title();
$thumbnail = get_thumbnail($width,$height,'thumb',$post_title,$post_title);
$thumb = $thumbnail["thumb"];
print_thumbnail($thumb, $thumbnail["use_timthumb"], $post_title, $width, $height, 'thumb'); ?></a>
<div class="title">
<h2><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
</div>
</li>
<?php endwhile; wp_reset_query(); ?>
Here is the loop for the feature boxes, which has four loops:
<?php query_posts("showposts=1&cat=".get_catId($instance['catOption4']));
while (have_posts()) : the_post(); $themename_do_not_duplicate[] = $post->ID;?>
...stufff
<?php endwhile; wp_reset_query(); ?>
Do I have to put the global themename_do_not_duplicate
before each loop here or can I just put it in the beginning of the widget?
Here is the main loop:
<?php global $themename_do_not_duplicate;
if ($instance['NoRepeatPost'] == 1) {$arrgs=array('posts_per_page'=>$instance['postNum'],'post__not_in' => $themename_do_not_duplicate,'paged'=>$paged,);}
else {$arrgs=array('posts_per_page'=>$instance['postNum'],'paged'=>$paged,);}
query_posts($arrgs);
if(have_posts()) : while (have_posts()) : the_post();$themename_do_not_duplicate[] = $post->ID; //begin the loop ?>
...stuff
<?php endif;wp_reset_query(); //and so ends the loop ?>
In the top of the header.php file I put global themename_do_not_duplicate
I hope I explained my problem thoroughly.
]]>Now here is the kicker: It actually works, but I am getting an error on my error log that says:
PHP Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/alvinr/public_html/wp-content/themes/moonandback/sidebar.php on line 67
Line 67 is where I am checking the $do_not_duplicate array to make sure I continue over any posts that are duplicates. Now it is actually keeping the duplicates from showing but my error log is getting filled up with all these errors (one for each post in the loop).
What is even more weird, is that I can not get it to generate this error every time. It seems to only do it the first time I load the page. Perhaps some cacheing is going on so that it is not duplicating the error on subsequent loads.
Anyone have any ideas?
]]>Here’s my issue:
I want the categories to show the most recent post in that category but not to duplicate the most recent post overall.
I implemented do_not_duplicate and it works great except it conflicts with ‘showposts=1’ so whichever category holds the most recent overall post shows up blank instead of showing the second most recent post. I know I need some additional parameters to have the categories offset by 1 if the most recent post overlaps but am at a loss on how to code.
Here’s my current code:
<?php $featured_query = new WP_Query('showposts=1');
while ($featured_query->have_posts()) : $featured_query->the_post();
$do_not_duplicate[] = $post->ID ?>
<!-- most recent post overall -->
<?php endwhile; ?>
<?php query_posts('category_name=red&showposts=1&offset=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php if (in_array($post->ID, $do_not_duplicate)) continue; update_post_caches($posts); ?>
<!-- most recent in red category -->
<?php endwhile; ?>
<?php rewind_posts(); ?>
<?php query_posts('category_name=blue&showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php if (in_array($post->ID, $do_not_duplicate)) continue; update_post_caches($posts); ?>
<!-- most recent in blue category -->
<?php endwhile; ?>
<?php rewind_posts(); ?>
<?php query_posts('category_name=yellow&showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php if (in_array($post->ID, $do_not_duplicate)) continue; update_post_caches($posts); ?>
<!-- most recent in yellow category -->
<?php endwhile; ?>
Thanks for the help!
]]>In my layout page, default.php I want to display ALL posts of all categories except the 5 posts displayed in my div featured at the top of my page.
I don’t to put in place the loop with $do_not_duplicate.
Help me please…
featured.php page :
<?php
$featuredcat = get_option('woo_featured_category'); // ID of the Featured Category
$the_query = new WP_Query('category_name=' . $featuredcat . '&showposts=5&orderby=post_date&order=desc');
$counter = 0;
while ($the_query->have_posts()) : $the_query->the_post(); $do_not_duplicate = $post->ID;
?>
<?php $counter++; ?>
<div class="featured" id="post-<?php the_ID(); ?>">
<?php if ( get_post_meta($post->ID, 'image', true) ) { ?> <!-- DISPLAYS THE IMAGE URL SPECIFIED IN THE CUSTOM FIELD -->
<div class="featured-img" style="background:url(<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&h=200&w=350&zc=1&q=95) top left no-repeat;">
<?php if ($counter == 1) { ?><div id="ribbon"><img src="<?php bloginfo('stylesheet_directory'); ?>/styles/<?php echo "$style_path"; ?>/ribbon.png" alt="A la une !!!" /></div><?php } ?> <!-- SHOW "FEATURED" RIBBON FOR FIRST POST ONLY -->
</div>
<?php } else { ?> <!-- DISPLAY THE DEFAULT IMAGE, IF CUSTOM FIELD HAS NOT BEEN COMPLETED -->
<div class="featured-img" style="background:url(<?php bloginfo('template_directory'); ?>/images/no-img-large.jpg) top left no-repeat;">
<?php if ($counter == 1) { ?><div id="ribbon"><img src="<?php bloginfo('stylesheet_directory'); ?>/styles/<?php echo "$style_path"; ?>/ribbon.png" alt="A la une !!!" /></div><?php } ?> <!-- SHOW "FEATURED" RIBBON FOR FIRST POST ONLY -->
</div>
<?php } ?>
<h2><?php the_category(', ') ?></h2>
<h3><a title="Lien permanent pour <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<p><?php echo strip_tags(get_the_excerpt(), '<a><strong>'); ?></p>
<p style="margin-bottom:0px !important;"><span class="continue"><a title="Lien permament pour <?php the_title(); ?>" href="<?php the_permalink() ?>">Continuer...</a></span></p>
</div><!--/featured-->
<?php endwhile; ?>
<div id="featured-th">
<ul class="idTabs">
<?php
$the_query = new WP_Query('category_name=' . $featuredcat . '&showposts=5&orderby=post_date&order=desc');
$counter = 0;
while ($the_query->have_posts()) : $the_query->the_post(); $do_not_duplicate = $post->ID;
?>
<?php $counter++; ?>
<?php if ( get_post_meta($post->ID, 'image', true) ) { ?> <!-- DISPLAYS THE IMAGE URL SPECIFIED IN THE CUSTOM FIELD -->
<li <?php if ($counter == 5) { ?>class="last"<?php } ?>><a href="#post-<?php the_ID(); ?>"><img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&h=57&w=100&zc=1&q=95" alt=""/></a></li>
<?php } else { ?> <!-- DISPLAY THE DEFAULT IMAGE, IF CUSTOM FIELD HAS NOT BEEN COMPLETED -->
<li <?php if ($counter == 5) { ?>class="last"<?php } ?>><a href="#post-<?php the_ID(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/no-img-thumb.jpg" alt=""/></a></li>
<?php } ?>
<?php endwhile; ?>
</ul>
</div><!--/featured-th-->
default.php page :
<div class="box">
<?php
include(TEMPLATEPATH . '/includes/version.php');
$the_query = new WP_Query('cat=-'. $ex_vid . '&showposts=' . $showposts . '&orderby=post_date&order=desc');
$counter = 0; $counter2 = 0;
while ($the_query->have_posts()) : $the_query->the_post(); $do_not_duplicate = $post->ID;
?>
<?php $counter++; $counter2++; ?>
<div class="post <?php if ($counter == 1) { echo 'fl'; } else { echo 'fr'; $counter = 0; } ?>">
<?php if ( get_post_meta($post->ID, 'image', true) ) { ?> <!-- DISPLAYS THE IMAGE URL SPECIFIED IN THE CUSTOM FIELD -->
<img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&h=57&w=100&zc=1&q=95" alt="" class="th" />
<?php } else { ?> <!-- DISPLAY THE DEFAULT IMAGE, IF CUSTOM FIELD HAS NOT BEEN COMPLETED -->
<img src="<?php bloginfo('template_directory'); ?>/images/no-img-thumb.jpg" alt="" class="th" />
<?php } ?>
<h2><?php the_category(', ') ?></h2>
<h3><a title="Lien permanent pour <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<p class="posted">Posté le <?php the_time('d F Y'); ?></p>
<p><?php echo strip_tags(get_the_excerpt(), '<a><strong>'); ?> <span class="continue"><a title="Lien permanent pour <?php the_title(); ?>" href="<?php the_permalink() ?>">Continuer...</a></span></p>
<p class="comments"><?php comments_popup_link('Aucun commentaire', '1 commentaire', '% commentaires'); ?></p>
</div><!--/post-->
<?php if ( !($counter2 == $showposts) && ($counter == 0) ) { echo '<div class="hl-full"></div>'; ?> <div style="clear:both;"></div> <?php } ?>
<?php endwhile; ?>
<div class="fix" style="height:20px"></div>
<p class="ar hl3"><a href="<?php echo bloginfo('wpurl').'/?page_id='.$GLOBALS['archives_id']; ?>" class="more">VOIR PLUS D'ARTICLES DANS LES ARCHIVES</a></p>
</div><!--/box-->
]]>