Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
  1. I have a page template, call it page-myblog.php, that I want to display an ordinary loop on.
  2. I have created a page 'myblog' in the admin area and it crops up as normal on the front end.
  3. I want to show all my blog posts in the loop on that page template. I refuse to instantiate a new WP_Query on the page and I want to edit the main query instead.

This appraoch has not worked thanks to the following behaviour:

add_action( 'pre_get_posts', function( $query ) {
    if( $query->query['pagename'] === 'myblog' ) {
        $query->set( 'pagename', null );
        $query->set( 'post_type', 'post' );
    }
});

results in the following SQL being generated.

SELECT   wp_posts.* FROM wp_posts  WHERE 1=1  AND wp_posts.post_type = 'post'  ORDER BY wp_posts.post_date DESC 

This query works, which is to say, it does find posts if you run it in an SQL console, but because it doesn't explicitly select IDs (I think?) there's nothing in my $query->posts array.

Why does this happen? And can I fix it?

share|improve this question
    
SELECT wp_posts.* means select all fields, including ID, however this isn't why the query doesn't work. my experience has been that you simply can't convert a page query into a query for posts via pre_get_posts. if you were able to succeed, what type of query would it be? it would no longer be is_page(), so your page template wouldn't load! –  Milo Mar 10 '13 at 21:13
    
if it's because the template hasn't been chosen, surely it should punt me back to index.php with an ordinary loop, then? –  djb Mar 10 '13 at 21:24
    
Is there some reason you can't go to wp_admin->Settings->Reading and pick this page as the "Posts Page"? –  s_ha_dum Mar 10 '13 at 22:24
    
the template thing was just one aspect of the overall issue- a page is a page and the query can't reliably be made to behave as anything else. the one exception is the page for posts option, which s_ha_dum mentions. –  Milo Mar 11 '13 at 0:36
add comment

1 Answer

I'm not sure why you don't want to use wp_Query? wp_Query is a very powerful function about retrieving various queries. First of all, pages in the blog do not relate with posts, so you won't get any predefined post query for that page-template that you created.

However there are a few ways to show post in your static page with custom query (eg. query_posts, get_posts, wp_Query).

In this case, if you don't want to use wp_Query. Please try with query_posts. As far as I understand you just want to retrieve original post type which is 'post'. not the custom post types. am I right?

Pls try this following code in your page-myblog.php and pls make sure you are using that template for that page.

<?php
$args = array(
     'post_type'      => 'post',
     'posts_per_page' => 5,
     'order'          => ASC
);
// The Query
query_posts( $args );

// The Loop
while ( have_posts() ) : the_post();
   echo '<li>';
   the_title();
   echo '</li>';
endwhile; ?>

// Reset Query wp_reset_query(); ?>

I hope this would help your problem. If you want to know more about query_posts please follow the given link. Good luck! http://codex.wordpress.org/Function_Reference/query_posts

share|improve this answer
    
Don’t use query_posts(). –  toscho Mar 11 '13 at 16:13
    
thanks for the answer, but it's not what i was looking for –  djb Mar 11 '13 at 17:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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