If you would like to talk about your project call (02) 4474 5861

Benefit of Using WP_Query

At times with WordPress the website may need to display custom information outside of the normal loop. The loop is what asks the database for information to display on a specific page. The following examples are showing the WordPress loop ask for the basic content of any posts that have ben added to the database.

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content('Read the rest of this entry »'); ?>
<?php endwhile; ?>
<?php else : ?>
<p>If no posts will display this text</p>
<?php endif; ?>

or can be coded like this too:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content('Read the rest of this entry »'); ?>\<?php endwhile; ?>
<?php else : ?>
<p>If no posts will display this text</p>
<?php endif; ?>

The above is nice and simple but at times the need to display only post information from a specific category or tag or combination of things may be needed. This is where WP_Query comes in handy.

WordPress Online Resources for the Loop

A Comprehensive Beginners Guide to the WordPress Loop

WordPress Custom Loop with WP_Query

By creating a custom loop using WP_Query you can be very selective about what type of information you would like to retrive from the database to display on a page on the website. By using an array of arguments (what items we would like to display in the loop) we can ask for a bunch of requirements.

The below example shows the WP_Query requesting only information / posts that are from the category of cats and that  have been tagged as having blue eyes.

// WP_Query arguments
$args = array (
'category_name' => 'cats',
'tag' => 'blue',
);
// The Query
$happycats = new WP_Query( $args );
// The Loop
if ( $happycats->have_posts() ) {
while ( $happycats->have_posts() ) {
$happycats->the_post(); // do something
}
} else {
// no posts found }
// Restore original Post Data
wp_reset_postdata();

WordPress Online Resources for WP_Query

Mastering WP_Query

Instinctively creative & thoughtful strategies with result orientated outcomes

Visually communicating effectively with your potential & current customers adds to the good business experience.

BJ2DESIGN combines a wealth of design knowledge with creative business strategies to create result oriented work.

Read More About BJ2DESIGN

creative hand and tools