I have created a new Block for the Wordpress Editor, and I am trying to get a simple WP_Query to work, but so far, the $args
in the query are mostly ignored, apart from the post_type
setting. For example, I want to set posts_per_page
to 3, but this is ignored. Why is this?
<?php
wp_reset_postdata();
wp_reset_query();
$latest_news_args = [
'post_type' => 'post',
'posts_per_page' => 2,
'post_status' => 'publish',
'nopaging' => true,
'ignore_sticky_posts' => true
];
$latest_news_query = new WP_Query($latest_news_args);
if ($latest_news_query->have_posts()) : while ($latest_news_query->have_posts()) : $latest_news_query->the_post();
$post_id = get_the_ID();
$post_title = get_the_title();
$post_url = get_the_permalink();
$post_excerpt = get_the_excerpt();
$post_image = get_the_post_thumbnail_url();
$post_image_id = get_post_thumbnail_id( $post->ID );
$post_image_alt_meta = get_post_meta ( $post_image_id, '_wp_attachment_image_alt', true );
$post_image_alt = $post_image_alt_meta ? esc_html($post_image_alt_meta) : $post_title; // If no alt text specified, fall back to post title
?>
<div class="latest-news-block__post">
<?php if($post_image): ?>
<img class="latest-news-block__post-image" src="<?php echo $post_image; ?>" alt="<?php echo $post_image_alt; ?>">
<?php endif; ?>
<h3 class="latest-news-block__post-title">
<a href="<?php echo $post_url; ?>"><?php echo $post_title; ?></a>
</h3>
<div class="latest-news-block__post-excerpt">
<p><?php echo $post_excerpt; ?></p>
<a href="<?php echo $post_url; ?>" class="latest-news-block__post-more more-link"><?php _e('Read more','site'); ?></a>
</div>
</div>
<?php endwhile; endif; wp_reset_postdata(); wp_reset_query(); ?>
I have created a new Block for the Wordpress Editor, and I am trying to get a simple WP_Query to work, but so far, the $args
in the query are mostly ignored, apart from the post_type
setting. For example, I want to set posts_per_page
to 3, but this is ignored. Why is this?
<?php
wp_reset_postdata();
wp_reset_query();
$latest_news_args = [
'post_type' => 'post',
'posts_per_page' => 2,
'post_status' => 'publish',
'nopaging' => true,
'ignore_sticky_posts' => true
];
$latest_news_query = new WP_Query($latest_news_args);
if ($latest_news_query->have_posts()) : while ($latest_news_query->have_posts()) : $latest_news_query->the_post();
$post_id = get_the_ID();
$post_title = get_the_title();
$post_url = get_the_permalink();
$post_excerpt = get_the_excerpt();
$post_image = get_the_post_thumbnail_url();
$post_image_id = get_post_thumbnail_id( $post->ID );
$post_image_alt_meta = get_post_meta ( $post_image_id, '_wp_attachment_image_alt', true );
$post_image_alt = $post_image_alt_meta ? esc_html($post_image_alt_meta) : $post_title; // If no alt text specified, fall back to post title
?>
<div class="latest-news-block__post">
<?php if($post_image): ?>
<img class="latest-news-block__post-image" src="<?php echo $post_image; ?>" alt="<?php echo $post_image_alt; ?>">
<?php endif; ?>
<h3 class="latest-news-block__post-title">
<a href="<?php echo $post_url; ?>"><?php echo $post_title; ?></a>
</h3>
<div class="latest-news-block__post-excerpt">
<p><?php echo $post_excerpt; ?></p>
<a href="<?php echo $post_url; ?>" class="latest-news-block__post-more more-link"><?php _e('Read more','site'); ?></a>
</div>
</div>
<?php endwhile; endif; wp_reset_postdata(); wp_reset_query(); ?>
The problem was that there were 'pinned' posts, which were overriding everything. Un-pinning these posts seemed to do the trick.
Also, thanks to Sally CJ for the nopaging
tip - I wasn't aware that it causes posts_per_page
to be ignored also.
echo $latest_news_query->request;
and see if the query is good. If it's not, it's probably because a plugin, the active theme or another custom code is filtering/altering the query. – Sally CJ Commented May 27, 2020 at 9:39wp_reset_postdata
andwp_reset_query
, these are unnecessary, and thewp_reset_query
function should only be used to cleanup a call toquery_posts
( never usequery_posts
orwp_reset_query
). All you need is a single call towp_reset_postdata
, it should go just after the post loop (endwhile;
but inside the if statement. – Tom J Nowell ♦ Commented May 27, 2020 at 9:46nopaging
included? – Tom J Nowell ♦ Commented May 27, 2020 at 9:48nopaging
, which if you enable it (i.e. disables pagination and theLIMIT
clause), theposts_per_page
will be ignored. – Sally CJ Commented May 27, 2020 at 10:37nopaging
was added, but that's a useful tip to remember! – wickywills Commented May 27, 2020 at 10:41