How do I order my loop query?

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

screenshot: the table

Read More

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.

   <?php
 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; ?>

Related posts

Leave a Reply

2 comments

  1. 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.