admin管理员组

文章数量:1332873

I am creating a theme, and have run into a problem with how a custom WP_Query seems to interact with the Blog pages show at most settings found under Settings -> Reading in the WordPress backend. I have a paged query, where - for layout reasons, I want to display 6 posts per page. My code looks like this:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 6,
    'orderby' => 'date',
    'order' => 'DESC',
    'paged' => $paged,
);

$loop = new WP_Query($args);
if ($loop->have_posts()):
    while ($loop->have_posts()): $loop->the_post();?>
                        <div class="blogpost-div">
                            My blogpost goes here
                        </div>
        <?php
    endwhile;
else:
    _e('There are currently no posts available.', 'textdomain');
endif;

echo custom_pagination($loop);
wp_reset_postdata();
?>

This works fine. However, if I have less than 6 published blog posts in my installation, this query displays no posts (Or if the next page doesen't have 6 posts, it 404s) unless the aforementioned reading setting is also set to be limited at 6 posts. This becomes an issue, if someone needs to install my theme on a fresh installation of WordPress, since they would then also need to adjust these settings.

In short - it seems, that when a custom query tries to modify posts_per_page, and the total amount of posts generated by the query is less than the defined number of posts in posts_per_page the query somehow conflicts with the "Blog pages show at most settings" if these aren't set to the same value.

Is this really intentional?

本文标签: wp queryBlog post per page setting conflicting with custom WPQuery