The WordPress codex says that:
query_posts()
is only one way amongst many to query the database and generate a list of posts. Before deciding to use query_posts(), be sure to understand the drawbacks.
but it does not list what those alternatives to query_posts()
are. Should I be using something else?
Take a look at this page. It lists a few alternatives to
query_posts
.WP_Query()
may be what you’re looking for.You could use
get_posts()
or create anew WP_Query
. There are a few more too that are a little more specific such aswp_get_recent_posts()
. Have a look throughwp-includes/post.php
to see some of the available ways to get posts. I can’t tell you if you should be using something else without knowing what you are trying to do.query_posts will update the global $wp_query. Maybe you don’t want that.
If you do $my_query = new WP_Query and then $my_query->query_posts you won’t mess with the globals.
Sometimes it’s easier to use get_posts()
Also you have the option to make your own sql requests.