Steffani
Steffani

Reputation: 33

How can i reset a query in a custom wordpress metabox

I'm working on this plugin for wordpress and i am stuck on a query that won't be reset. In the following function:

function WPSM_artists_autocomplete(){

 $response = array();
 
 query_posts('post_type=artist&posts_per_page=-1');
  
  if (have_posts()) : while (have_posts()) : the_post();
   $image_id = get_post_thumbnail_id();  
  $image_url = wp_get_attachment_image_src($image_id,'artist-icon');  
  $image_url = $image_url[0];  
  
  $response[] = array( get_the_ID() , get_the_title() , null, '<img src="'.$image_url.'" />'. get_the_title()); 
    endwhile; endif;
  
    wp_reset_query();
    
    // Write JSON file
 $output = json_encode($response);
 $data = WPSM_CACHE_DIR."/data.json";
 $fh = fopen($data, 'w') or die("can't open file");
 fwrite($fh, $output);
 fclose($fh);
 
 // Return JSON url
 echo WPSM_CACHE_URL."/data.json";
}

I use a query_posts to populate a metabox. But the wp_reset_query(); doesn't seem to work properly. This affects all other metaboxes and post related option. The global $post variable is set to the latest value of this query, and not the default value of the posts edit page.

I'd love to hear how to solve this plugin. Could use everything to get me in the right direction.

Upvotes: 3

Views: 2592

Answers (1)

Jamie
Jamie

Reputation: 61

I came across this today and found a fix.

You will need to store the original $post before starting a new loop and then at the end of your function you will need to set it back.

Before you function assign $post to a temporary variable.

$original_query = $wp_query;

Then at the end of your function set it back.

   $wp_query = $original_query;
   wp_reset_postdata();

Not sure if the above works in your case as I was using a custom query.

I have posted my code below so you can have a look.

            global $wpdb;
            global $post;
            $originalpost = $post;

            $querydetails = "
                SELECT *
                FROM $wpdb->posts
                WHERE $wpdb->posts.post_type = 'projects'
                AND $wpdb->posts.post_status = 'publish'
                ORDER BY $wpdb->posts.post_date DESC
             ";

             $pageposts = $wpdb->get_results($querydetails, OBJECT);

             if ($pageposts) {
                 foreach ($pageposts as $post) { 
                       setup_postdata($post);

                        $postID = get_the_ID();
                        echo '<option value="';
                        echo $postID . '"';
                        foreach ($meta as $m) {
                            if ($postID == $m) echo ' selected="selected" ';
                        }               
                        echo '>';
                        echo the_title();
                        echo '</option>';
                 }
            }

            echo "</select>";
            $this->show_field_end($field, $meta);
            $post = $originalpost;

Upvotes: 6

Related Questions