-
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:
- WP_Query vs get_posts
- Get posts by meta data OR title
- Ordering Posts by parent category, name ascending
- When should you use WP_Query vs query_posts() vs get_posts()?
- 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”
- Modify WordPress Search
- When to use WP_query(), query_posts() and pre_get_posts
- Display upcoming events in the next 30 days by querying posts with timestamp in custom meta
- Optimising WP_Query with ACF Fields
- meta_query where the meta value is not the post title
- Elementor posts custom query based on tag [closed]
- Query post and sort by custom value calculated with custom field on cpt
- Post incorrectly excluded when using “category__in”?
- Query multiple post types, but different order for each
- search.php to search only the post title
- How to add date_query to meta_query array
- Why my query is not “Main_query”?
- Multi-layered WP_Query
- `offset` WP_Query argument dont work via `pre_get_posts`
- Display posts ONLY from the current logged in user and current week/year/month Elementor posts
- Query post ordered by meta value that show first post that contained a tag
- How do i create a custom post query when the meta value is an array?
- get_posts query is taking about 40 seconds to execute
- Pagination of custom page with custom fields query
- Query pages created by deleted users
- query hook parse_tax_query function takes no effect
- Populate select list with meta values from all posts of a Custom Post Type
- Form checkbox value going to dynamic URL
- Duplice post with standard WP loop – fixed by using query_posts() instead
- ElasticPress is (aparently) messing with my search filters
- setting offset to category number in archive page
- Woocommerce Get Orders By Meta Value
- Any way to use FETCH_KEY_PAIR with $wpdb?
- Exclude post from wp_query based on custom field boolean
- How to do WP_User_Query for Nested Array value Inside Nested Array field
- How do I stop the same post showing multiple times in a archive?
- Query for current post
- Doing $wpdb->get_results returns NULL, doing the same query in my DB returns correct value
- get_posts return only first result
- Related posts queries
- Custom query to retrieve oldest post and retrieve others with date interval
- Move some posts to end of sort order, even if there is a sort in the wp_Query already
- Adding nofollow to all the post links in get_posts with pre_get_posts or WP_Query
- Meta query orderby meta_value_num sorting by role first