wp query - posts_per_page not working in block

admin2025-01-07  3

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(); ?>
Share Improve this question asked May 27, 2020 at 9:26 wickywillswickywills 1471 silver badge15 bronze badges 11
  • Try 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:39
  • I notice at the top of your file you call wp_reset_postdata and wp_reset_query, these are unnecessary, and the wp_reset_query function should only be used to cleanup a call to query_posts ( never use query_posts or wp_reset_query ). All you need is a single call to wp_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:46
  • 1 Also, you mention this is a block, but the code snippet has no block related code, is this a single file or is there surrounding code being ommitted for brevity? Is this a server rendered block? Why was nopaging included? – Tom J Nowell Commented May 27, 2020 at 9:48
  • 1 @wickywills, sorry, I didn't really notice that nopaging, which if you enable it (i.e. disables pagination and the LIMIT clause), the posts_per_page will be ignored. – Sally CJ Commented May 27, 2020 at 10:37
  • 1 Thanks @SallyCJ - in my case it didn't work before nopaging was added, but that's a useful tip to remember! – wickywills Commented May 27, 2020 at 10:41
 |  Show 6 more comments

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258425a511.html

最新回复(0)