Title: Query Help
Last modified: August 19, 2016

---

# Query Help

 *  Resolved [nadine00](https://wordpress.org/support/users/nadine00/)
 * (@nadine00)
 * [16 years ago](https://wordpress.org/support/topic/query-help/)
 * Hi,
 * I am just looking for some help on a query. So far I have it pulling the correct
   posts, based on a meta field / value. But I am having problems doing the post
   data setup. I received some good advice from some helpful posters in another 
   thread, tried out somethings in the wpdb example file, and got some help from
   the google group for magic fields, but I’m stumped, and was wondering if anyone
   has a suggestion.
 * Here’s the code:
 *     ```
       <?php
       		       // go to the database and get the a certian meta key. Find a value similar to X in the serialized array
       		        $query = "SELECT * FROM $wpdb->postmeta WHERE meta_key = 'grade-type' AND meta_value like '%Post Secondary%'";
       		                 $rows = $wpdb->get_results($query);
       		                foreach($rows as $row){
       		                       setup_postdata($row);
       		                       // Pull remaining post data from correct database
       		                       $curpost = $row->post_id;
       		                       $curquery = "SELECT * FROM $wpdb->posts WHERE ID = '$curpost'";
       		                       $currow = $wpdb->get_results($curquery);
       		                       setup_postdata($currow);
       		                       echo "the post title:" . $currow->post_title;
       		                       print_r($currow); //just for testing
       		                  }
       		               ?>
       ```
   
 * And here’s the link to the beta site (please ignore any oddness, its still in
   the works)
 * [http://work.nadinelessio.com/firstsecond/build/teaching-resources/making-comics/](http://work.nadinelessio.com/firstsecond/build/teaching-resources/making-comics/)
 * Thanks!
 * Nadine

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [dralezero](https://wordpress.org/support/users/dralezero/)
 * (@dralezero)
 * [16 years ago](https://wordpress.org/support/topic/query-help/#post-1487181)
 * If you use `$currow = $wpdb->get_row($curquery);` instead it will grab that one
   row directly into your variable instead of as a result set. Right now you are
   grabbing the result for the post as a result set and then not accessing the row.(
   Im pretty sure that’s right).
 * Have you been looking at this?
    [http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query)
 * It shows how to inner join the two tables to grab everything in one query to 
   loop through.
 *  Thread Starter [nadine00](https://wordpress.org/support/users/nadine00/)
 * (@nadine00)
 * [16 years ago](https://wordpress.org/support/topic/query-help/#post-1487303)
 * I was looking at JOIN, I’m not sure how it would be worked into my current query
   setup tho. I can tinker with is a bit, but if you have any suggestions on formatting,
   I would appreciate it. Also thanks for the note on $currow. I will try it out!
   🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Query Help’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [postmeta](https://wordpress.org/support/topic-tag/postmeta/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [strings](https://wordpress.org/support/topic-tag/strings/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [nadine00](https://wordpress.org/support/users/nadine00/)
 * Last activity: [16 years ago](https://wordpress.org/support/topic/query-help/#post-1487303)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
