Last active
May 20, 2016 16:25
-
-
Save davidvandenbor/12a0b9cb3c9204b83fb5 to your computer and use it in GitHub Desktop.
WordPress: WP_query loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* WP_QUERY, STANDARD | |
Because of how WordPress maintains information using global variables, | |
it's crucial that once you've crafted, executed, and processed your | |
own WP_Query function, you need to call this particular function to | |
restore information to the state that it was in prior to when you ran | |
your own query. This is so that WordPress continue looping through | |
information as needed in the template hierarchy, and so that data | |
isn't mangled or lost when rendering content later in a page | |
or on another page! | |
In other words: If you use the_post() with your query, | |
you need to run wp_reset_postdata() afterwards to have | |
Template Tags use the main query's current post again. | |
===================================================================================================================*/ | |
?> | |
<?php /* WP_QUERY, "IF-ELSE" style :-) (OFFICIAL VERSION) ===============================================================*/ ?> | |
<?php | |
// args | |
$args = array( | |
'numberposts' => -1, | |
'post_type' => 'paintings', | |
'meta_key' => '', | |
'meta_value' => '', | |
'orderby'=> 'date' | |
); | |
// query | |
$custom_query = new WP_Query( $args ); | |
?> | |
<?php if ( $custom_query->have_posts() ) : ?> | |
<!-- the loop --> | |
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?> | |
<h2><?php the_title(); ?></h2> | |
<?php endwhile; wp_reset_postdata(); //end loop and reset wp_query ?> | |
<!-- end of the loop --> | |
<?php else: ?> | |
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> | |
<?php endif; ?> | |
<?php /* WP_QUERY, "SHORTHAND" STYLE! ===========================================================================*/ ?> | |
<ul> | |
<?php $david_query = new WP_Query( 'cat=3&posts_per_page=19&post_type=any' ); // define the loop | |
while ( $david_query->have_posts()) : $david_query->the_post(); // start of the loop ?> | |
<li> | |
<?php the_post_thumbnail('thumbnail')?> | |
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> | |
<?php the_excerpt(); ?> | |
<a href="<?php the_permalink() ?>" class="button"> | |
<span class="edit-link"> Read more!</span> | |
</a> | |
</li> | |
<?php endwhile; wp_reset_postdata(); //end loop and reset wp_query ?> | |
</ul> | |
<?php | |
/** | |
* examples of meta key calls | |
* @author @david | |
* ================================================================================================================= | |
*/ | |
?> | |
<?php // standard example: | |
$david_args = array( | |
'cat' => 22, | |
'meta_query' => array( | |
array( | |
'key' => 'color', | |
'value' => 'white' | |
) | |
) | |
); | |
$david_query = new WP_Query( $david_args ); ?> | |
<?php if ( $david_query->have_posts() ) : ?> | |
<!-- the loop --> | |
<?php while ( $david_query->have_posts() ) : $david_query->the_post(); ?> | |
<h2><?php the_title(); ?></h2> | |
<?php endwhile; ?> | |
<!-- end of the loop --> | |
<?php wp_reset_postdata(); ?> | |
<?php else: ?> | |
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> | |
<?php endif; ?> | |
<?php | |
// example: all the posts EXCEPT the ones with meta key «color» and meta value «white»: | |
$rd_args = array( | |
'meta_query' => array( | |
array( | |
'key' => 'color', | |
'value' => 'white', | |
'compare' => '!=' | |
) | |
) | |
); | |
$rd_query = new WP_Query( $rd_args ); ?> | |
<?php | |
// Example: all the posts with white or green color custom field value: | |
$rd_args = array( | |
'meta_query' => array( | |
array( | |
'key' => 'color', | |
'value' => array('white','green'), | |
'compare' => 'IN' | |
) | |
) | |
); | |
$rd_query = new WP_Query( $rd_args ); ?> | |
<?php | |
// Example: all the posts (products in online shop for example) EXCEPT white products and green products: | |
$rd_args = array( | |
'meta_query' => array( | |
array( | |
'key' => 'color', | |
'value' => array('white','green'), | |
'compare' => 'NOT IN' | |
) | |
) | |
); | |
$rd_query = new WP_Query( $rd_args ); ?> | |
<?php | |
// Example: the product price is more than 2000 and less than 4000 | |
$rd_args = array( | |
'meta_query' => array( | |
array( | |
'key' => 'price', | |
'value' => array( 2000, 4000 ), | |
'type' => 'numeric', | |
'compare' => 'BETWEEN' | |
) | |
) | |
); | |
$rd_query = new WP_Query( $rd_args ); ?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment