I have a query that spits out posts that aren't featured but for some reason the posts_per_page
limiter isn't working...
<?php $featured_posts = get_posts( [
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'fields' => 'ids',
] );
query_posts( array( 'post__not_in' => $featured_posts ) );
while ( have_posts() ) : the_post();?>
<div class="news-item-block col-md-4" role="article">
<a class="news-item-image-link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('news-grid-image'); ?>
</a>
<span class="news-item-date"><?php the_date( 'M d, Y' ); ?></span>
<a class="news-item-title" href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php endwhile;
wp_reset_query();
?>
Thanks for any insight on this issue.
Got it. The trick was to target the while function e.g.:
<?php $i = 1; while (have_posts() && $i < 4) : the_post();?>
<?php the_post_thumbnail(); ?>
<?php the_date( 'M d, Y' ); ?>
<?php the_title(); ?>
<?php $i++; endwhile;?>
Yeah, use 'nopaging' => true
and 'ignore_sticky_posts'=>true
<?php $featured_posts = get_posts( [
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'nopaging' => true,
'ignore_sticky_posts'=>true,
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'fields' => 'ids',
] );
query_posts( array( 'post__not_in' => $featured_posts ) );
while ( have_posts() ) : the_post();?>
<div class="news-item-block col-md-4" role="article">
<a class="news-item-image-link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('news-grid-image'); ?>
</a>
<span class="news-item-date"><?php the_date( 'M d, Y' ); ?></span>
<a class="news-item-title" href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php endwhile;
wp_reset_query();
?>
The problem you are facing here is that you are getting posts object in $featured_posts
and thus calling query_posts
is really unnecessary. From your code structure, I am assuming that you are trying to access the main query but with modified parameters. A better approach to accomplish your goal is as follows.
$args = array(
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'meta_compare' => '!=',
'fields' => 'ids',
);
$featured_posts = new WP_Query( $args );
while ( $featured_posts->have_posts() ) : $featured_posts->the_post();?>
<div class="news-item-block col-md-4" role="article">
<a class="news-item-image-link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('news-grid-image'); ?>
</a>
<span class="news-item-date"><?php the_date( 'M d, Y' ); ?></span>
<a class="news-item-title" href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php
endwhile;
wp_reset_query();
On a side note, I'd advise against using query_posts
as it directly modifies the main query. But hopefully the above code should help you achieve desired results.