0

From problem to problem, disco to disco :P

My template requires that i print out headlines of 3 last blogposts in the header of my template. Its single page layout so those blogposts will link to posts that will be on page anyway - just somewhere down the line...

Since im complete wordpress newbie - i've already required help with creating this whole single page layout:

Single page theme

Modified home page query does not yield expected results

Long story short - i created app hook, that changes original query to include all pages. These blogposts need to be taken care of before we get to parsing the pages.

What i tried - following some examples i set up something like this:

            <?php
            $args = array( 
                'orderby' => 'ID', 
                'order' => 'DESC', 
                'post_type' => 'post',
                'post_status' => 'publish',
                );
            $children = get_pages($args);
            foreach( $children as $post ){
                setup_postdata($post);
                get_template_part( 'header', 'blogheader' );
            }
            wp_reset_postdata();
            ?>

Yes the query does not include the posts limit 3 - but so be it. What this gives me - exactly nothing. If i comment everything out from those args, then i get my main query. Even just one argument, like 'post_type' => 'post' - i get nothing.

This led me into thinking that all those args are just modifying the original main query, not creating new one. Fine. I replaced get_pages($args) with new WP_Query( $args );. Now i got funny results - whole load of posts - even though i actually have just one, original, "hello world" post. It prints out even posts that have no title (im guessing they are drafts or something, even though i have not created any draft blogposts).

So what i want - i thought that setting post_type to post will give me blogposts only - i was wrong. What parameters do i have to set to get only published blogposts, sorted by id?

Alan

1 Answer 1

2

You were not passing any limit to the query.

    $args = array( 
        'posts_per_page' => 3, 
        'post_type' => 'post',
        'post_status' => 'publish',
        'ignore_sticky_posts' => true
    );
    $children = new WP_Query($args);
    if ($children->have_posts()) {
      while ($children->have_posts()) {
        $children->the_post();
        // get_template_part( 'header', 'blogheader' ); // not on my server :)
      }
    }
    wp_reset_postdata();

I made a couple of changes. You were querying for 'posts' with a function called get_pages. While that function does accept a post_type argument using WP_Query is neater.

I added ignore_sticky_posts. Without that you won't get the latest posts, you'll get sticky posts and then the latest ones.

I don't know what is in header-blogheader.php but it is possible that it is written in such a way that things still do not work.

6
  • blogheader only prints out the title and date of the blogpost. nothing else. Ill try your solution tomorrow. Thanks.
    – Zayatzz
    Jun 30, 2013 at 20:31
  • My concern is that variables like $post can get out of scope.
    – s_ha_dum
    Jun 30, 2013 at 20:34
  • Tested - works like a charm. Can i get an explanation now please. Why does $children->have_posts() work and $children as $post does not?
    – Zayatzz
    Jul 1, 2013 at 14:56
  • With your code, that would work but not with 'post_type' => 'post',. With my code you would need foreach ($children->posts as $post) { but it should work.
    – s_ha_dum
    Jul 1, 2013 at 15:08
  • Hmm that made no sense to me. In my post i also said that i replaced get_posts for new WP_Query( $args ) - so our args and query were pretty much the same. Your loop got posts though and mine didnt. Thats what i was wondering about. Why your loop worked and mine didnt.
    – Zayatzz
    Jul 1, 2013 at 15:14

Your Answer

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

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