Widget with Query

Here we will discuss about Wordpress plugins development and their support
  • Ads

Widget with Query

Postby benny » Mon Aug 09, 2010 12:37 am

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.
benny
Newbie
Newbie
 
Posts: 95
Joined: Mon Jul 26, 2010 6:12 am

Re: Widget with Query

Postby benny » Mon Aug 09, 2010 12:37 am

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); ?>
benny
Newbie
Newbie
 
Posts: 95
Joined: Mon Jul 26, 2010 6:12 am

  • Ads

Return to Wordpress Plugins

Who is online

Users browsing this forum: No registered users and 1 guest

cron