I can't get the loop to work as it's supposed to, without using the not-so-recommended:

<?php query_posts('post_type=post&post_status=publish&paged='. get_query_var('paged')); ?>

I'm using a custom page template named "template-blog.php" with this content: http://pastebin.com/raw.php?i=9W3akEcX

And here's the "content.php" file: http://pastebin.com/raw.php?i=EH6xhzLQ

Using that loop, I get only one post, which is actually a page to which I've assigned the "template-blog.php" template, in the form of a post - only the title, since I haven't added any content to it when I created it the dashboard. Is there anything wrong with my code above? Thanks.

  • 1
    please check and repair the pastebin links.
    – Michael
    Commented Aug 30, 2013 at 16:14
  • 2
    Please do not use pastebin links. Paste the code here.
    – vancoder
    Commented Aug 30, 2013 at 16:15
  • Is it against the rules or why? Commented Aug 30, 2013 at 17:31
  • Why do you want to make it harder for people to help you? And in a year's time, when those pastebin links no longer work, what good will your question be to other people?
    – vancoder
    Commented Aug 30, 2013 at 22:20

2 Answers 2


The problem is not that the loop is not working. The problem is your code is not executing the loop the way you want it to.

PHP does not do what you mean. It does exactly what you tell it to do. Like most computer languages, it is just an extremely fast and incredibly dumb list processor.

When you removed the query_posts() call you needed to replace it with something else that tells PHP what you want. PHP doesn't just "know" anything.

Here is one solution. At the bottom of your template file, replace this:

<!-- START footer -->
<?php get_footer(); ?>

With this:

<!-- START footer -->


add_action( 'pre_get_posts', 'wpse_112282_change_query' );

function wpse_112282_change_query( $query ) {

    if ( $query->is_main_query() ) {

        $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

        $query->set( 'post_type', 'post' );
        $query->set( 'post_status', 'publish' );
        $query->set( 'paged', $paged );


This uses one recommended way to get the value of the $paged and follows the rest of the query you used in the query_posts() call. The first argument in query_set() is the value in your query that is to the right of the equal sign (=).

For example, post_status=publish becomes:

$query->set( 'post_status', 'publish' );

Read more about using the 'pre_get_posts' action.

  • Hey Charles, thanks for your response, but it didn't work... pastebin.com/ak6RGjTv Why can the twentytwelve theme work fine with only using this thing? <?php if(have_posts()) : while(have_posts()) : the_post(); ?> Commented Aug 30, 2013 at 17:34
  • Sorry. Move the add_action line to near the top of the file. Commented Aug 30, 2013 at 23:54
  • It still didn't work... Commented Aug 31, 2013 at 8:39

I finally managed to solve this problem ( without having to use <?php query_posts('post_type=post&post_status=publish&paged='. get_query_var('paged')); ?> ) by renaming the "template-blog.php" into "home.php", and after that I set the template of the page where I wanted my post to be displayed (in my case, "Blog") to "Default Template" and then I went to Settings -> Reading and selected "Blog" as my posts page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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