Popular Categories

Query Loops for WordPress

Logic Behind the Loop:

The loop initiate with the logic to first check whether the loop belongs to a single page or listing page. If the loop belongs to the listing page then it will list all the items in the blog and if it is a single page then it will just display post.

If no post is available then loop has the option to display a custom message or a 404 page as per your need.

Thanks to WordPress templates, the loop can be customized for just about every page or type of content on your site.

Simple Query Loop for displaying blog list with title and excerpt:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<h2><?php the_title() ;?></h2>
<?php the_excerpt(); ?>

<?php endwhile; else: ?>

<p>Sorry, no posts to list</p>

<?php endif; ?>

Query Loop for displaying blog list with title and content:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<h2><?php the_title() ;?></h2>
<?php the_content(); ?>

<?php endwhile; else: ?>

<p>Sorry, no posts to list</p>

<?php endif; ?>

Query Loop for displaying single page with title and content:

As mentioned earlier that the loop very much remain same for the single page, so similar code can be used for displaying the post over a single page.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<h2><?php the_title() ;?></h2>
<?php the_content(); ?>

<?php endwhile; else: ?>

<p>Sorry, no posts to list</p>

<?php endif; ?>

Basic Query Loop with Pagination:

<?php

global $wp_query;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array( 'post_type' => 'posts','posts_per_page' => 6,'paged' => $paged) ;

query_posts( $args );
while ( have_posts() ) : the_post();

echo '<li>';

the_title();

echo '</li>';

endwhile;

?>

Using Query Loop to filter custom taxonomies in custom post type:

<?php

query_posts( array(
'post_type' => 'myportfoliotype',
'paged' => $paged,
'posts_per_page' => 80,
'tax_query' => array(
array(
'taxonomy' => 'category', //or tag or custom taxonomy
'field' => 'id',
'terms' => array('9')
)
)
) );

?>

Using Query Loop with advance custom fields:

<?php
query_posts( array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'event_id',
'value' => '1',
'compare' => '=',
)
)
));
?>

Using Single Custom field with advance custom fields for querying:

<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
);

// query
$the_query = new WP_Query( $args );

?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('event_thumbnail'); ?>" />
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>

Hopefully! You can now say yourself a champ in Query loops. Will keep updating this post, as it will help alot of us.