WordPress Query Loop , Query_Posts

Another way to get posts is using the query_posts() function.

You can filter the posts with any type, category, author, tag and anything really.

It is a bit more advance way to filter and get posts.

lets say you have a movie post type and you want to get Bruce Campbell and Chuck Norris movies. Easy;

 

<?php
$args = array(
    'post_type'=> 'movie',
    'actor'    => 'Bruce Campbell, Chuck Norris',
    'order'    => 'ASC'
);
query_posts( $args );
while ( have_posts() ) : the_post();
?>

// your the_title, the_content and stuff goes here

<?php
endwhile;
wp_reset_query();
?>

 

query_posts will be very useful when you are working with custom post types and custom taxonomies.

You cant filter stuff on a normal loop.

 

There is so many argument and parameters to use full list here -> https://developer.wordpress.org/reference/classes/wp_query/#parameters

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments