WordPress TutorialRemove Version from WordPress and StylesheetsRun WordPress local with XAMPPSecurity in WordPress - EscapingSecurity in WordPress - SanitizationThe Loop (main WordPress loop)Update WordPress ManuallyWordPress Actions and FiltersWordPress add action()WordPress add editor style()WordPress add menu page()WordPress Add ShortcodeWordPress add submenu page()WordPress add theme support()WordPress Add/remove contact info for users with user contactmethods filter hookWordPress Admin Dashboard WidgetsWordPress AJAXWordPress Alternating main loop (pre get posts filter)WordPress Child Theme BasicsWordPress Create a Post ProgrammaticallyWordPress Create Template for Custom Post TypeWordPress Creating a custom templateWordPress Custom exerpts with excerpt length and excerpt moreWordPress Custom Post TypesWordPress Customizer Basics (Add Panel, Section, Setting, Control)WordPress Customizer Hello WorldWordPress DebuggingWordPress Enqueuing scriptsWordPress Enqueuing StylesWordPress Function : wp trim words()WordPress Function: add action()WordPress get bloginfo()WordPress get home path()WordPress get option()WordPress get permalink()WordPress get template part()WordPress get the category()WordPress get the title()WordPress home url()WordPress How Can I integrate Markdown editor with Advance Custom Fields repeater Add-on.WordPress initWordPress Installation and ConfigurationWordPress Making network requests with HTTP APIWordPress Meta BoxWordPress Options APIWordPress Plugin creationWordPress Plugin developmentWordPress Post FormatsWordPress Querying postsWordPress Remove Auto Line Breaks from Content and ExcerptWordPress REST APIWordPress Secure your installationWordPress ShortcodeWordPress Shortcode with attributeWordPress ShortcodesWordPress SidebarsWordPress Site MigrationWordPress TaxonomiesWordPress Template hierarchyWordPress template includeWordPress the $wpdb ObjectWordPress the Admin Bar (aka The Toolbar)WordPress the title()WordPress theme and child-theme developmentWordPress ThemesWordPress wp get current user()WordPress WP Query() LoopWordPress WP-CLIWordPress WP-Cron

WordPress Querying posts

From WikiOD

Syntax[edit | edit source]

  • $the_query = new WP_Query( $args );
  • $posts_array = get_posts( $args );

Parameters[edit | edit source]

Parameter Description
$args (array) An array of needed arguments for a query - can be custom tailored to your needs, e.g. querying posts from only one category, from custom post type or even querying certain taxonomy

Remarks[edit | edit source]

Query arguments are numerous. WP_Query() codex page has a list of parameters. Some of them are

One of the most important thing to have in mind is:

Never use query_posts()[edit | edit source]

query_posts() overrides the main query, and can cause problems in the rest of your theme. Any time you need to modify the main query (or any query for that matter) is to use pre_get_posts filter. This will allow you to modify the query before it ran.

Also when you are querying posts, you should always reset it using wp_reset_postdata(). This will restore the global $post variable of the main query loop, and you won't have any issues later on (such as categories being excluded, because in your secondary loop you've excluded them and forgot to reset the query).

Using WP_Query() object[edit | edit source]

Creating a separate instance of the WP_Query object is easy:

$query_args = array(
                'post_type' => 'post',
                'post_per_page' => 10

$my_query = new WP_Query($query_args);

if( $my_query->have_posts() ):
    while( $my_query->have_posts() ): $my_query->the_post();
       //My custom query loop


Notice that you need to build the query arguments array to your specification. For more details, look at WP_Query codex page.

Using get_posts()[edit | edit source]

get_posts() is a wrapper for a separate instance of a WP_Query object. The returned value is an array of post object.

global $post;

$args = array(
    'numberposts' => 5,
    'offset'=> 1,
    'category' => 1

$myposts = get_posts( $args );

foreach( $myposts as $post ) :
    setup_postdata($post); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endforeach;
wp_reset_postdata(); ?>

For more info check out the get_posts() codex page.