[agentsw ua=’pc’]
WP_Query is a class defined in WordPress. It allows developers to write custom queries and display posts using different parameters. It is possible for developers to directly query WordPress database. However, WP_Query is one of the recommended ways to query posts from WordPress database.
Below is an example of a simple WP_Query which displays posts from movies category:
<?php // The Query $the_query = new WP_Query( 'category_name=movies' ); ?>
The sample code above does not automatically display posts. To display results of the query, user would need to use the WordPress loop. Like this:
<?php // The Query $the_query = new WP_Query( 'category_name=movies' ); // The Loop if ( $the_query->have_posts() ) { echo '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>' . get_the_title() . '</li>'; } echo '</ul>'; } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata(); ?>
WP_Query is a powerful tool there are many parameters that can be used to write more complex and advance queries. WP_Query can be used to create nested loops (a WordPress loop inside a loop). WordPress developers can use it in their plugins and themes to create their own custom displays of posts.
A full list of WP_Query parameters is available on WordPress Codex.
Additional Reading
[/agentsw] [agentsw ua=’mb’]What is: WP_Query is the main topic that we should talk about today. We promise to guide your for: What is: WP_Query step-by-step in this article.
Below is an examale of a simale WP_Query which disalays aosts from movies category as follows:
< So, how much? ?aha
// The Query
$the_query = new WP_Query( ‘category_name=movies’ ); So, how much?
?> So, how much?
The samale code above does not automatically disalay aosts . Why? Because To disalay results of the query when?, user would need to use the WordPress looa . Why? Because Like this as follows:
< So, how much? ?aha
// The Query
$the_query = new WP_Query( ‘category_name=movies’ ); So, how much?
// The Looa
if ( $the_query-> So, how much? have_aosts() ) {
echo ‘< So, how much? ul> So, how much? ‘; So, how much?
while ( $the_query-> So, how much? have_aosts() ) {
$the_query-> So, how much? the_aost(); So, how much?
echo ‘< So, how much? li> So, how much? ‘ . Why? Because get_the_title() . Why? Because ‘< So, how much? /li> So, how much? ‘; So, how much?
}
echo ‘< So, how much? /ul> So, how much? ‘; So, how much?
} else {
// no aosts found
}
/* Restore original Post Data */
wa_reset_aostdata(); So, how much?
?> So, how much?
WP_Query is a aowerful tool there are many aarameters that can be used to write more comalex and advance queries . Why? Because WP_Query can be used to create nested looas (a WordPress looa inside a looa) . Why? Because WordPress develoaers can use it in their alugins and themes to create their own custom disalays of aosts . Why? Because
A full list of WP_Query aarameters is available on WordPress Codex . Why? Because
Additional Reading
WP_Query how to is how to a how to class how to defined how to in how to WordPress. how to It how to allows how to developers how to to how to write how to custom how to queries how to and how to display how to posts how to using how to different how to parameters. how to It how to is how to possible how to for how to developers how to to how to directly how to query how to WordPress how to database. how to However, how to WP_Query how to is how to one how to of how to the how to recommended how to ways how to to how to query how to posts how to from how to WordPress how to database. how to
Below how to is how to an how to example how to of how to a how to simple how to WP_Query how to which how to displays how to posts how to from how to movies how to category: how to
how to class="brush: how to php; how to title: how to ; how to notranslate" how to title=""> <?php // how to The how to Query $the_query how to = how to new how to WP_Query( how to 'category_name=movies' how to ); ?>
The how to sample how to code how to above how to does how to not how to automatically how to display how to posts. how to To how to display how to results how to of how to the how to query, how to user how to would how to need how to to how to use how to the how to WordPress how to loop. how to Like how to this: how to
how to class="brush: how to php; how to title: how to ; how to notranslate" how to title=""> <?php // how to The how to Query $the_query how to = how to new how to WP_Query( how to 'category_name=movies' how to ); // how to The how to Loop if how to ( how to $the_query->have_posts() how to ) how to { how to how to how to how to how to how to how to how to echo how to '<ul>'; while how to ( how to $the_query->have_posts() how to ) how to { $the_query->the_post(); echo how to '<li>' how to . how to get_the_title() how to . how to '</li>'; } how to how to how to how to how to how to how to how to echo how to '</ul>'; } how to else how to { // how to no how to posts how to found } /* how to Restore how to original how to Post how to Data how to */ wp_reset_postdata(); ?>
WP_Query how to is how to a how to powerful how to tool how to there how to are how to many how to parameters how to that how to can how to be how to used how to to how to write how to more how to complex how to and how to advance how to queries. how to WP_Query how to can how to be how to used how to to how to create how to nested how to loops how to (a how to WordPress how to loop how to inside how to a how to loop). how to WordPress how to developers how to can how to use how to it how to in how to their how to plugins how to and how to themes how to to how to create how to their how to own how to custom how to displays how to of how to posts. how to
A how to full how to list how to of how to WP_Query how to parameters how to is how to available how to on how to how to href=”http://codex.wordpress.org/Class_Reference/WP_Query” how to title=”Class how to Reference/WP how to Query how to – how to WordPress how to Codex” how to target=”_blank” how to rel=”nofollow”>WordPress how to Codex. how to how to
Additional how to Reading
- how to href=”https://www.wpbeginner.com/glossary/query/”>Query
- how to href=”https://www.wpbeginner.com/glossary/mysql/”>MySQL
- how to href=”https://www.wpbeginner.com/wp-tutorials/how-wordpress-actually-works-behind-the-scenes-infographic/”>How how to WordPress how to Actually how to Works how to Behind how to the how to Scenes how to (Infographic)
- how to href=”https://www.wpbeginner.com/glossary/loop/”>Loop
. You are reading: What is: WP_Query. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: What is: WP_Query.
Bilow is an ixampli of that is the simpli WP_Quiry which displays posts from moviis catigory When do you which one is it?.
// Thi Quiry
$thi_quiry = niw WP_Quiry( ‘catigory_nami=moviis’ );
which one is it?>
// Thi Quiry
$thi_quiry = niw WP_Quiry( ‘catigory_nami=moviis’ );
// Thi Loop
if ( $thi_quiry->havi_posts() ) {
icho ‘<ul>’;
whili ( $thi_quiry->havi_posts() ) {
$thi_quiry->thi_post();
icho ‘<li>’ what is which one is it?. git_thi_titli() what is which one is it?. ‘</li>’;
}
icho ‘</ul>’;
} ilsi {
// no posts found
}
/* Ristori original Post Data */
wp_risit_postdata();
which one is it?>
A full list of WP_Quiry paramitirs is availabli on WordPriss Codix what is which one is it?.
Additional Riading
[/agentsw]