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
- Some doubts about how the main query and the custom query works in this custom theme?
- Wp get all the sub pages of the parent using wp query
- How to query for most viewed posts and show top 5
- Usage of the new “posts_clauses” filter in WordPress 3.1?
- WP_Query: query posts by ids from array?
- Order posts by ID in the given order
- get_template_part in for loop
- 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
- WordPress Custom Query to show posts from last x years
- Identify which loop you are hooking into; primary or secondary?
- Perform query with meta_value date
- WP_Query ordered by custom field that is a date string?
- 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?
- How to find out what “Blog pages show at most” is set to [duplicate]
- meta_query on a date range using an array of values
- WP_query category__in not working, only pulls from first category
- Pass the same object to multiple widgets in a template with one query
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- How to Loop within a Loop (Display Children and then Grandchildren)
- Retrieve or Query Pages by ID
- How do you query wordpress posts using a math formula between multiple meta field values?
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- Page navigation doesn’t show when query category
- 2 loops on page – one with orderby rand second orderby date
- Undefined WP_Query::has_posts()?
- How do you Query posts with nothing in common?
- 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
- if/else on custom query gives 200 OK when condition not met?
- Creating a custom search for a specific post type
- Query posts by specific word on title
- 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
- Multiple loops without repeating content
- WP_Query condition affects posts_per_page count
- Query last updated posts (posts updated in the last 24 hours)
- Custom Query num_rows returns wrong amount
- add active class based on permalink and url
- 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
- How to get list of posts from permalinks?
- Query Page Content From Theme Options?
- Check return value of get_posts
- 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?
- Transient api Caches confused
- How can I override one post and make it display content for another post?
- Best choice for multiple loop in page?
- Get all posts which was posted on X Days WordPress
- 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]
- Conditional operator OR not working with custom fields
- complex get_posts() query to select child pages
- Get posts that do not have the same tags as current
- 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
- Posts query according to meta box date
- How to access $wp_query object from array of posts given by get_posts?
- Function using get_posts() with tax_query not working when called from functions.php
- 2 queries with counters
- I am officially missing something about transient posts
- 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?