Skip to content
Advertisement

Looping through WordPress posts using a query + have_posts() only returns a fraction of them

I am trying to create a custom HTML sitemap. I am using the following PHP code to retrieve all the posts in a website:

         $args = array(
        'post_type' => 'post'
    );

    $post_query = new WP_Query($args);

    if($post_query->have_posts() ) {
        while($post_query->have_posts() ) {
            $post_query->the_post();
            ?>
            <a href=" <?php the_permalink(); ?>"><?php the_title(); ?></a>
            <?php
            }
        }

    wp_reset_postdata();

For some reason, the above code is only printing the links of the 10 most recent posts. I have more than 30 posts on my website. As I am not very comfortable with PHP, is there something wrong with the above code? Is there a different way I can try to achieve the same output? Thank you.

Advertisement

Answer

In order to show more posts on one page, also the regular WordPress settings can be adapted, in the backend under Settings > Reading > Blog pages show max. XX pages (exact terms could be a bit different, I am usually using another language)

Alternatively you can add an according parameter in the query arguments (the $args array), for example 'posts_per_page' => 30, .

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement