The difference between get_posts & WP_Query
You can view get_posts()
as a slimmed down WP_Query
. In fact looking at the source:
//... prepares query array $r
$get_posts = new WP_Query;
return $get_posts->query($r);
get_posts()
use WP_Query
, but only returns an array of posts – nothing more. Furthermore it sets:
$r['no_found_rows'] = true;
Normally (by default with WP_Query
object) – WordPress queries how many posts there are in total – even if you are only after the first 10. It does this so it can perform pagination. So get_posts()
is actually (slightly) quicker (it also ignores sticky posts).
Which to use…
If you only need an array of posts, and don’t need the query object -use get_posts()
. Otherwise, if you do need the access to the query object methods, or pagination, or sticky posts at the top, you should use WP_Query
.
Related Posts:
- Ordering Posts by parent category, name ascending
- When should you use WP_Query vs query_posts() vs get_posts()?
- Query Custom Meta Value with Increment
- Get posts by meta data OR title
- Include current post into loop
- get_posts output always same post
- Should I reset $wp_query?
- Magazine style frontpage with multiple categories/loops and no duplicate posts
- When should you use WP_Query vs query_posts() vs get_posts()?
- When to use WP_query(), query_posts() and pre_get_posts
- How to query for most viewed posts and show top 5
- WP_Query: query posts by ids from array?
- Order posts by ID in the given order
- pre_get_posts with get_posts
- How-to exclude terms from the main query the most performant way?
- Get the number of posts from the current page results
- Identify which loop you are hooking into; primary or secondary?
- Meta Query with date and time on the same Day before given time
- Retrieving 3 latest post from each of 5 different custom post types
- Transient pagination not working properly
- get_posts() seemingly ignoring post_type
- Can not switch the queried post in pre_get_posts hook
- How to query ‘posts_per_page’ to display a different blog posts index template?
- meta_query on a date range using an array of values
- Pass the same object to multiple widgets in a template with one query
- How to Loop within a Loop (Display Children and then Grandchildren)
- How do you query wordpress posts using a math formula between multiple meta field values?
- Page navigation doesn’t show when query category
- 2 loops on page – one with orderby rand second orderby date
- Undefined WP_Query::has_posts()?
- Create ONE callback for all page templates, post filter queries + paginated pages, triggering pagination via AJAX
- Get posts for last working week in WP_Query
- Creating a custom search for a specific post type
- How to query a custom post type with a taxonomy filter but display post type archive page?
- Five posts from a category in footer
- Change query_posts to WP_Query in page but does not work
- WP_Query condition affects posts_per_page count
- Query last updated posts (posts updated in the last 24 hours)
- Weird orderby => post__in issue
- Slow page loads due to WordPress Core Query
- Order by empty custom field
- Check if loop has any categories?
- Some doubts about how the main query and the custom query works in this custom theme?
- Conflict with get_posts and the_content
- Query Page Content From Theme Options?
- Reset WordPress Post Query to default
- Is_single() conditional tag returns null in query
- How do I rewrite this loop as a new WP_Query style-loop?
- Best choice for multiple loop in page?
- How to provide meta_key array to wp_query?
- Filter post query to only show direct children of category
- Use WP_Query or query_posts() or get_posts() for optimizing a site?
- How to create custom query by keyword in post title?
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- complex get_posts() query to select child pages
- How to give classname to post if post has no content?
- How to filter query loop block with a search string from the query parameters
- Function using get_posts() with tax_query not working when called from functions.php
- Why doesn’t my WP Meta Query return any results?
- Paginate pages with dynamic query
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- Determine if ID is page or post and query the ID
- How to query for a page, get data, then query for child pages of that page
- Custom template for password protected page
- Prioritising and Ordering Posts By Category Name Using A Custom Loop
- Filter products on category AND tag
- Facing problem with tax_query results
- Populate select list with meta values from all posts of a Custom Post Type
- Duplice post with standard WP loop – fixed by using query_posts() instead
- pre_user_query vs pre_get_posts
- How do I stop the same post showing multiple times in a archive?
- Get posts that match defined arrays of tags
- How to get meta key list efficiently?
- I need query_posts() to order results first by a meta value and then by post ID
- WP_Query & Duplicate entries
- WordPress For Loop Prints Unwanted Extra Paragraph Element
- How to show post title in content editor in backend?
- My entry results are not consistently alphabetized
- Merge two queries and remove duplicate
- How to order posts, that have already been filtered by custom taxonomy, by their category names?
- query_posts() doesn’t seem to be called in my page
- Some doubts about how the main query and the custom query works in this custom theme?
- WP_Query secondary query failing
- Both WP_Query and get_posts returning 1 post
- Optimising specific Query with ACF meta objects
- Loop through multiple custom fields with increasing number
- Why I obtain this WP_Query strange behavior trying to select posts having a specific tag?
- How to organize a WP_Query’s list of posts by category and display category title?
- meta_query dates from an array
- Retrieve posts in custom post type and specific taxonomies
- Converting an existing query_posts to WP_Query
- WordPress Query showing multiple titles
- Query Posts by date range with fixed beginning and end
- custom query to get posts
- Multiple loop for “featured” items returns wrong posts
- How do I correctly query posts from a post ID?
- tax_query not working properly with get_posts
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- A Depth Like Parameter For “get_posts”
- Show number of posts AND number on current page (cannot make it work)