Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the PhotoSmash plugin to let users upload their own images and vote on them, and it works great. On one of my pages I use the following code to list the posts that have a certain category:

    <?php
if (is_page() ) {
$category = get_post_meta($posts[0]->ID, 'category', true);
}
if ($category) {
  $cat = get_cat_ID($category);
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $post_per_page = -1; // -1 shows all posts
  $do_not_show_stickies = 1; // 0 to show stickies
  $args=array(
    'category__in' => array($cat),
    'orderby' => 'date',
    'order' => 'ASC',
    'paged' => $paged,
    'posts_per_page' => $post_per_page,
    'caller_get_posts' => $do_not_show_stickies
  );
  $temp = $wp_query;  // assign orginal query to temp variable for later use   
  $wp_query = null;
  $wp_query = new WP_Query($args); 
  if( have_posts() ) : 
        while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <section class="vote6" id="post-<?php the_ID(); ?>">
          <?php the_content(); ?>
          <section class="voter">
              <?php DisplayVotes(get_the_ID()); ?>
          </section>
        </section>
    <?php endwhile; ?>
  <?php else : ?>

        <h2 class="center">Not Found</h2>
        <p class="center">Sorry, but you are looking for something that isn't here.</p>
        <?php get_search_form(); ?>

    <?php endif; 

    $wp_query = $temp;  //reset back to original query

}  // if ($category)
?>

This works great as well, except when the PhotoSmash plugin is activated. I get the following error:

Fatal error: Call to a member function get() on a non-object in \nas-001\winspace004\10-3mpromos.ca\www\carcrazy\wp-includes\query.php on line 27

Line 27 is return $wp_query->get($var); The issue is somewhere inside of bwb-photosmash.php in the plugin's main folder, but I cannot figure out where. Has anyone else come across this issue?

share|improve this question
    
wow. removing "$wp_query = null;" seems to fix everything.. –  Carson Jan 11 '11 at 18:33
add comment

1 Answer

up vote 2 down vote accepted

There is no need to juggle $wp_query object, store it in temp, etc. It is rarely good idea to directly mess with important global variables, unless you absolutely need to.

You can just create your own arbitrary variable and init it with new WP_query

$some_variable = new WP_Query($args);

and so on.

Also don't forget to cleanup after with wp_reset_query().

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.