• Hello,

    I’m creating a page that puts needs to put two post types and several categories in different sections of the page:

    – 1 post carrying the tag ‘featured’ should be on the top in a featured div section.

    – After this, a custom post type (events) should show 5 posts in its own div.

    – Then, only the most recent or sticky post of each category of the ‘post’ type should be shown in its own div section. ( Imagine four columns, each representing the most recent post or sticky post from that category).

    – Last but not least, the normal loop, so including all categories, should be shown, again in its own div.

    I was wondering whether this setup would be possible to do with get_posts() and if this is the better option of the two. I’m fairly new to altering the loop (I’m a front-end guy), so some help would be greatly appreciated.

    If my explanation is insufficient, please consult this link for a wireframe:
    http://community.flagshipwp.com/uploads/default/82/b8c463448291b0a9.png

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter stnick

    (@stnick)

    Okay, I believe I found the answer here, a confirmation would still be appreciated:
    http://beebell.co.uk/wordpress-queries-when-to-use-wp_query-get_posts-query_posts-pre_get_posts/

    Also, it would be really helpful if someone could explain whether there is a huge performance difference between the two? Should I worry about this kind of thing when making websites that will most likely not exceed 500 posts in the next 3 years?

    Moderator bcworkz

    (@bcworkz)

    Yes, you can use either get_posts() or the WP_Query class, running a different custom loop for each of your preliminary content. The main loop remains the final index loop, so don’t forget to reset the postdata after each custom loop so the main loop will still work correctly.

    The difference between the two as described by beebell, while true, is a bit simplistic. get_posts() is basically just a procedural wrapper for a WP_Query class method. As a wrapper, it of course incurs some additional overhead and is less efficient. In the scheme of all things WordPress, this overhead is quite minimal.

    There would be no difference due to the size of the site since the same query is run either way. If you had a very heavily trafficked site, the extra bit of wrapper overhead could make some very minimal difference.

    Despite there being nearly no difference, I still recommend using the WP_Query methods simply because they are the most direct. And isn’t OOP supposed to be the “cool” way to code? 😉

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Use pre_get_posts or wp_query’ is closed to new replies.