The querying posts means to modify/filter the query of a page or add new query in the page. There are lot of ways to modify query of a page or post by replacing it with new instance of the query.
You can achieve this to query a wordpress post or post by using these wordpress built-in wordpress method.
- query_posts
- WP_Query
- get_posts
- pre_get_posts
query_posts() is simplest way to modify the main mysql query of a page by replacing it with new instance of the query. It is inefficient and will outright fail in some circumstances (especially often when dealing with posts pagination).
get_posts() is very similar in use and accepts same parameters, but it returns an array of wordpress posts, this function does not modify any wp global variables.
WP_Query is a in-built class powers the wordpress, to use this you can create and work with own object of it. Bit more complex, less restrictions, also safe to use anywhere in theme & plugin.
pre_get_posts is a wp filter, for altering/modifying any query. It is most often used to alter only the main query.
Please check attached image for the flow of all type of methods.
Example:
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo '
';
the_title();
echo '';
endwhile;
// Reset Query
wp_reset_query();
0 Comment(s)