0

I am pretty new in WordPress theme development and I am finding 2 problem with this posts archive page: http://lnx.asper-eritrea.com/archivio/

The previous page have to show ALL the blog posts into an unordered list.

The main problem is that are shown only the last 10 published posts.

The other problem is related to the warning caused of an erroneus call to the query_posts() function.

This is the PHP code that generate the posts loop:

<?
// get the term using the slug and the tag taxonomy
//$term = get_term_by( 'slug', 'featured', 'post_tag' );
// pass the term_id to tag__not_in
query_posts();
?>


<?php
if (have_posts()) :
    echo '<ul>';
    // Start the Loop.
    while (have_posts()) : the_post();

        /*
         * Include the post format-specific template for the content. If you want to
         * use this in a child theme, then include a file called called content-___.php
         * (where ___ is the post format) and that will be used instead.
         */

        echo '<li>';
        get_template_part('contentArchive', get_post_format());
        echo '</li>';

    endwhile;
    echo '</ul>';

else :
    // If no content, include the "No posts found" template.
    get_template_part('content', 'none');

endif;
?>

So why the previuos loop show only the last 10 records? How can I fix it and show all the blog post?

What can I do to fix the query_posts() call?

I think that maybe the 2 problems are related togheter because maybe I have to use a paramether to say to show all blog posts.

  • I have already explained this is one of my previous answers to one of your questions. See here – Pieter Goosen Aug 14 '14 at 16:24
2

WordPress by default will only load 10 posts. That can be changed in the admin under Settings->Reading and then setting the amount beside "Blog pages show at most".

You can override that number in your custom queries a couple of ways but I would first recommend against the use of query_posts() ( take a read at the link for the reasons why) in favour of get_posts() or even better WP_Query() and pass the appropriate arguments

//create a query with WP_Query
$posts = new WP_Query(array(
    'post-type' => 'post', //this can be post, page or a custom post type
    'posts_per_page' => -1, //this can be any number or settting to -1 will give you all the posts
    //add whatever other parameters you need
));

//modify the loop slightly
if ($posts->have_posts()) :
    echo '<ul>';
    // Start the Loop.
    while ($posts->have_posts()) : $posts->the_post();

        /*
         * Include the post format-specific template for the content. If you want to
         * use this in a child theme, then include a file called called content-___.php
         * (where ___ is the post format) and that will be used instead.
         */

        echo '<li>';
        get_template_part('contentArchive', get_post_format());
        echo '</li>';

    endwhile;
    echo '</ul>';

else :
    // If no content, include the "No posts found" template.
    get_template_part('content', 'none');

endif;
?>

Another way to do this would be to hook into the pre_get_posts action with allows you to hijack the loop and make changes to the query before the posts are pulled from the database.

You would need to check to be sure you are only affecting the main loop for that archive page by doing some checking using a function like is_main_query() and others mentioned at the bottom of that page.

Place this code in your functions.php file - you'll need to do some testing to be sure it's not affecting other pages/loops but that is the general idea.

function wst_157845( $query ) {
    if ( !is_admin() && $query->is_main_query() && is_archive() )
        $query->set( 'posts_per_page', -1 );
    }
}
add_action( 'pre_get_posts', 'wst_157845'); 

Hope this helps!

  • When using pre_get_posts on archive pages, you should use the check !is_admin(), otherwise your changes will affect the back end as well. – Pieter Goosen Aug 14 '14 at 16:56
  • 1
    Nice catch! Answer updated – Welcher Aug 14 '14 at 16:57
0

Try this:

query_posts( 'posts_per_page=-1' );

Just a head up, you shouldn't be using this function here, do whatever you want, but is recommender to use WP_Query or get_posts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.