-
query_posts()
is overly simplistic and a problematic way to modify the main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of thepre_get_posts
hook, for this purpose. TL;DR don’t use query_posts() ever. -
get_posts()
is very similar in usage and accepts the same arguments (with some nuances, like different defaults), but returns an array of posts, doesn’t modify global variables and is safe to use anywhere. -
WP_Query
is the class that powers both behind the scenes, but you can also create and work with your own instance of it. A bit more complex, fewer restrictions, also safe to use anywhere.
Related Posts:
- When should you use WP_Query vs query_posts() vs get_posts()?
- WP_Query vs get_posts
- Get posts by meta data OR title
- Ordering Posts by parent category, name ascending
- When to use WP_query(), query_posts() and pre_get_posts
- Wp get all the sub pages of the parent using wp query
- How to query for most viewed posts and show top 5
- Query Custom Meta Value with Increment
- 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
- Can not switch the queried post in pre_get_posts hook
- How to find out what “Blog pages show at most” is set to [duplicate]
- WP_query category__in not working, only pulls from first category
- Pass the same object to multiple widgets in a template with one query
- 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
- How do you Query posts with nothing in common?
- 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?
- Change query_posts to WP_Query in page but does not work
- 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
- Query Page Content From Theme Options?
- Check return value of get_posts
- Reset WordPress Post Query to default
- How can I override one post and make it display content for another post?
- Include current post into loop
- Get all posts which was posted on X Days WordPress
- Filter post query to only show direct children of category
- get_posts output always same post
- Use WP_Query or query_posts() or get_posts() for optimizing a site?
- complex get_posts() query to select child pages
- Should I reset $wp_query?
- How to give classname to post if post has no content?
- Posts query according to meta box date
- Why doesn’t my WP Meta Query return any results?
- How to query for a page, get data, then query for child pages of that page
- Magazine style frontpage with multiple categories/loops and no duplicate posts
- Prioritising and Ordering Posts By Category Name Using A Custom Loop
- Filter products on category AND tag
- Facing problem with tax_query results
- pre_user_query vs pre_get_posts
- 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
- How to show post title in content editor in backend?
- My entry results are not consistently alphabetized
- Merge two queries and remove duplicate
- Optimising specific Query with ACF meta objects
- How to organize a WP_Query’s list of posts by category and display category title?
- Retrieve posts in custom post type and specific taxonomies
- How to query post ids liked by the Author
- WordPress Query showing multiple titles
- Query Posts by date range with fixed beginning and end
- custom query to get posts
- A Depth Like Parameter For “get_posts”
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- Query Posts in a Predefined Order
- How to make an activities stream mixing posts and comments?
- Retrieving 3 latest post from each of 5 different custom post types
- How to query for a week using key => value WP_Query argument notation?
- How to use filter hook posts_join for querying taxonomy terms in posts_where?
- How to detect custom query inside `posts_where` hook?
- Is temporarily overwriting $wp_query a bad idea?
- WP query exclude post within the last month / only show over 1 month old
- WordPress query for most recent posts from multiple categories
- Too many posts cause slow load when paginating
- How to output different posts per page?
- Pass array of taxonomy terms to wp_query
- List of the years with posts presented
- WP_Query “OR”: Find posts by ID, Name or Post Title?
- WP_Query condition affects posts_per_page count
- Editing the default wordpress search
- Set global $wp_query/$post variable for dynamic page generation
- posts_per_page option limits the number of Gallery items
- Is there a better way to pull in custom content without querying posts?
- Query posts that contain video?
- Show a 404 error page if Public query variable’s value doesn’t exist?
- “paged” in WP_Query returns 0 posts
- Check if searched number is within the post meta value
- Notice thrown when creating numeric pagination on custom query
- Creating attachments archive in tags and categories
- Why always one post missing
- setting offset to category number in archive page
- Query for current post
- Query Posts | Combining multiple form inputs into query arguments to generate one filter result
- Sorting query_posts() with a complex orderby filter
- What is the best way to reset a search on a meta_key / meta_value?
- Slow query when selecting with large meta query or post__in
- Bring a post to the top of the query if it’s in a certain category?
- Both WP_Query and get_posts returning 1 post
- Using WP_Query to re-query and sort results using a date?
- Pagination is not working on custom query inside a custom home page template
- WordPress pagination returns the same posts
- new WP_Query with order args – no more distinction between categories