Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Wordpress Querying Posts

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 341
    Comment on it

    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.

    1. query_posts
    2. WP_Query
    3. get_posts
    4. 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.


    // The Query
    query_posts( $args );
    // The Loop
    while ( have_posts() ) : the_post();
        echo '
  • '; the_title(); echo '
  • '; endwhile; // Reset Query wp_reset_query();
    Wordpress Querying Posts

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: