Title: Widget with Query
Last modified: August 19, 2016

---

# Widget with Query

 *  Resolved [docnzok](https://wordpress.org/support/users/docnzok/)
 * (@docnzok)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/widget-with-query/)
 * I would like to create a sidebar widget with a custom query from the database.
   I have a field UserScore (a custom field) and want to retrieve the top 5 highest
   scores and the post title it relates to and place them in a sidebar.
 * I have generated the correct query including the join from post_meta to posts.
   I just don’t know how to place it.
 * I have tried a couple different plugins [Get Custom Field Values], [Query Posts]
   and [Widget Logic] but I just cannot get this to work.
 * If I only have 1 sidebar, should I just place the query in there? I don’t know
   how to do that.
 * MUCH APPRECIATED AS ALWAYS on this forum.
 * Mark-Chicago

Viewing 1 replies (of 1 total)

 *  Thread Starter [docnzok](https://wordpress.org/support/users/docnzok/)
 * (@docnzok)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/widget-with-query/#post-1612282)
 * Occasionally, when you are really stuck, you start over again from square one
   and when you solve the problem, you feel really, really good.
 * Here’s the solution:
    <?php $querystr = “ SELECT * FROM $wpdb->posts as wpost
   INNER JOIN $wpdb->postmeta ON (wpost.ID = $wpdb->postmeta.post_id) AND $wpdb-
   >postmeta.meta_key = ‘CustomField’ ORDER BY $wpdb->postmeta.meta_value DESC LIMIT
   5 “; $pageposts = $wpdb->get_results($querystr, OBJECT); ?>

Viewing 1 replies (of 1 total)

The topic ‘Widget with Query’ is closed to new replies.

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [widget](https://wordpress.org/support/topic-tag/widget/)

 * 1 reply
 * 1 participant
 * Last reply from: [docnzok](https://wordpress.org/support/users/docnzok/)
 * Last activity: [15 years, 10 months ago](https://wordpress.org/support/topic/widget-with-query/#post-1612282)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
