0

I realize that this seems to be a common problem and I've read through a lot of the posts but it's just not making sense. I was wondering if someone could take a look at my code.

What I am trying to do is build a home page that pulls posts from my custom post types into one loop. It's looking like almost every article that I read is calling the pagination based off of categories. In my case I just have completely separate post types and I am calling it by the type.

http://pastie.org/7378084

Thank you in advance

  • is this the main query on your home page? – Milo Apr 9 '13 at 0:58
  • yes and each of these post types have their own loop on separate pages. – Kris Tryber Apr 9 '13 at 1:10
2

For altering the main query, the preferred method is to use the pre_get_posts action rather than calling query_posts in the template. It's fairly safe to say at this point that any use of query_posts in the template is simply wrong, though sadly you will see a million examples of it all over the web.

The main query happens before the template is loaded, and WordPress makes the decision about what to do based on the result of that query, so doing things with the query in the template is going to be fraught with potential error. It's also a waste of resources to run a second query that overwrites the original, best to alter the query before it happens.

So here we use the conditional tags to check if we're on the front page and running the main query. It's important to note that this action is executed on every query, so we have to be explicit about which one we want to alter.

function wpa_post_types_front_page( $query ) {
    if ( $query->is_front_page() && $query->is_main_query() ) {
        $query->set( 'post_type', array(
            'post',
            'product',
            'flights',
            'touristdestinations'
        ) );
    }
}
add_action( 'pre_get_posts', 'wpa_post_types_front_page' );

Now WordPress will query all of your post types, it will know what page it's on, and how many pages exist, so pagination functions in the template should just work without having to do anything further.

  • Milo, Thanks for posting this. I haven't seen anything like this in all of my research yet. My main question is once I use this my homepage would just need to use the standard loop correct? Then in that standard loop I would probably need to call the wpa_post_types_front_page function at some point right? – Kris Tryber Apr 9 '13 at 1:47
  • yes, correct. this code would go in your theme's functions.php file, then you can just run the standard vanilla loop in the template. – Milo Apr 9 '13 at 1:49
  • so now I have this and when i don't have a "static front page" set it works like a champion. But when I set my "home" page as my static front it's blank. pastie.org/7378883 – Kris Tryber Apr 9 '13 at 1:57
  • ah, you have a static front page. unfortunately that means you need to create a new query via WP_Query. – Milo Apr 9 '13 at 15:36
  • Ok sweet milo thanks that's what i've been seeing. Either way your advice last night got me A LOT farther so thank you! – Kris Tryber Apr 9 '13 at 19:03
0

Ok so here is what I've come up with now. I just cannot get page navigation in there at all.

http://pastebin.com/1w3th07P

This actually shows the next posts link but that link just reloads the same page but my url changes like it should to page 2.

Any help would be appreciated, thank you.

Kris

  • The paging functions look at the main WP_Query object, see my old response here, i believe the information i supplied there may still apply. – t31os Apr 12 '13 at 10:35

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.