Wordpress WPQuery

As far as I can tell, using WPQuery in Wordpress is a big pain in the ass and sometimes it doesn't seem to work like you think it should. After reading the documentation, watching demo videos, and experimenting with the code, here's my best guess as to how to use it:
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query(array(
  'post_type' => 'loox_career',
  'posts_per_page' => 10,
  'paged' => $paged,
  'order' => 'ASC',
  'orderby' => 'title'
  ));
if ($wp_query->have_posts()) :
  ?>
  <h2><strong>Careers</strong></h2>
  <div class="content-wrapper block clearfix">
  <?php $wp_count = 0; ?>
  <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <div class="loox-careers <?php ($p_count%2)?print "even":print "odd";?>">
      <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
      <?php
      // get the post object
      global $post;
      // get the custom data including the custom fields
      $post_custom = get_post_custom($post->ID);
      // sanitize custom fields
      foreach ($post_custom as $key => $value) {
        $post_custom[$key] = wp_kses_post($value[0]);
      }
      // store field values
      $field_values['location'] = $post_custom['location'];
      ?>
      <div class="excerpt"><?php the_excerpt(); ?></div>
    </div>
  <?php endwhile; ?>
  </div>
  <div class="navigation">
    <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
    <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
  </div>
  <?php
endif;
$wp_query = $temp;
$temp = null;
?>
 
<?php
/**
 * Wordpress - WP_query
 */
 
// if you do:
$my_query = new WP_Query($query);
 
// you can do:
while ($my_query->have_posts()):
 $my_query->the_post();
endwhile;
wp_reset_postdata();
 
 
// These types of functions should be in just about every Wordpress functions.php file, says Jeff
// How to easily alter the query of pages by using the pre_get_posts action
// * The pre_get_posts action will pass in $query automatically
// * doing things this way will alter every single query on the page, not just the main one
// ** DON'T USE THIS FUNCTION, USE THE ONE IN THE NEXT SECTION BECAUSE YOU SHOULD ALWAYS JUST
//    MODIFY THE MAIN QUERY
 
function mahoney_alter_home( $query ) {
 global $wp_the_query;
 if ($query->is_home()) {
   $query->set( 'author', '-5' );
  }
 }
 add_action( 'pre_get_posts', 'mahoney_alter_home' );
 
// Using $wp_the_query, which holds the main query, and comparing it to $query,
// we can just alter the main query on a page
// You really always want to check if you are modifying the main query
// If you don't just affect the main query, then template don't really have control over other queries
//  that they may do
 
function mahoney_alter_home( $query ) {
 global $wp_the_query;
 if ($wp_the_query === $query && $query->is_home()) {
   $query->set( 'author', '-5' );
  }
 }
 add_action( 'pre_get_posts', 'mahoney_alter_home' );
 
/*
In wordpress 3.3+, Rather than:
$wp_the_query === $other_query_object
 
You can call:
$other_query_object->is_main_query( )
 
is_main_query( ), the function, will act on $wp_query, like any other conditional tag.
*/
 
/*
Calling the_post( )?
wp_reset_query( ) will reset $wp_query and the globals.
 
Calling $my_query->the_post( )?
wp_reset_postdata( ) will reset the globals.
*/
 
/*
$wp_query is always the main query, unless you use query_posts( ).
Restore it with wp_reset_query( ).
You know what? Just don't ever use query_posts() says Andrew Nacin, a core WP dev; use WP_Query()
*/
 
/*
bit.ly/wpsequery
this page has a diagram of how the queries are run
*/
?>

Article Type

General