I have a table in my database containing vote a tally I'm trying to order posts by.

screenshot: the table

I've tried something like this, which brings up all the voted posts and pagination okay but does not order them by DESC. Any advice if I'm approaching this right is appreciated.

 global $wpdb;
$my_posts = $wpdb->get_col("SELECT like_pid FROM wp_likes_count ORDER BY like_count");
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post__in' => $my_posts['like_pid'],
    'paged' => $paged,
    'posts_per_page' => 6,

    'order' => 'DESC'
query_posts($args); ?>

               <?php while (have_posts()) : the_post(); ?>    
               <!-- do stuff -->
               <?php endwhile; ?>

2 Answers 2


The Problem

$my_posts doesn't contain any posts. It contains post IDs and likes. And you're dropping both into post__in, which can't work. I guess even the number of posts wouldn't work if the plugin(?) wouldn't add any post with a 0-x integer to the db-table per default.

The Debug

Try the following:

$my_likes = $wpdb->get_col("SELECT like_pid FROM wp_likes_count ORDER BY like_count DESC");
echo '<pre>';
print_r( $my_likes );
echo '</pre>';

which will likely show you an array containing two sub-arrays.

Way to a solution

Then try to get the sub-array you need and drop it into your query. Example: 'post__in' => $my_likes['like_pid'].

The Plugin

Anyway, a plugin should store something like this in the post_meta table in a field and not add an unnecassary table for something like that. There should be a facebook plugin by otto in the wp.org repo. This one's much stronger, better written & by a trustable author. You should consider switching.

  • I'm only a learner so still working on your solution... it is a custom plugin with a few features unseen in others, so I'll have to have it worked on. The array I got back was ( [0] => 6 [1] => 37 [2] => 9 [3] => 33 [4] => 36 [5] => 32 [6] => 70 [7] => 8 [8] => 27 [9] => 69 [10] => 53 [11] => 7 [12] => 72 ) I tried to use your example code without luck, I thought maybe $my_likes['like_count'] but no.
    – DOA
    Commented Jun 9, 2011 at 14:34
  • It should be fine. You're getting back an array containing all the post IDs as values and 'post__in' does implode the array. Could you update your Q with the exact current code?
    – kaiser
    Commented Jun 9, 2011 at 14:46
  • @kaiser I've updated the first to what I'm trying.
    – DOA
    Commented Jun 9, 2011 at 14:53
  • and yes the array from your debug code was showing the Post ID in the correct order by amount of votes.
    – DOA
    Commented Jun 9, 2011 at 14:55
  • Could it be that you have another loop before it (it's not your main loop)?
    – kaiser
    Commented Jun 9, 2011 at 14:57

User the parameter 'order' in your loop call.

$args = array(
    'post__in' => $my_posts,
    'paged' => $paged,
    'posts_per_page' => 6,
    'order' => 'DESC'

There is also a order_by parameter


  • ..No luck there, it seems to be ordering by post date no matter what I try.
    – DOA
    Commented Jun 9, 2011 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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