WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a plugin which displayed the top most voted posts in a list with the vote count.

The code is placed here: https://gist.github.com/hchouhan/6727356

        function dot_most_recommended_posts($numberOf, $before, $after, $show_count, $post_type="showcase" ) {
            global $wpdb;

            $request = "SELECT * FROM $wpdb->posts, $wpdb->postmeta";
            $request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";
            $request .= " AND post_status='publish' AND post_type='$post_type' AND meta_key='_recommended'";
            $request .= " ORDER BY $wpdb->postmeta.meta_value+0 DESC LIMIT $numberOf";
            $posts = $wpdb->get_results($request);

                foreach ($posts as $item) {
                    $post_title = stripslashes($item->post_title);
                    $permalink = get_permalink($item->ID);
                    $post_count = $item->meta_value;
                    echo $before.'<a href="' . $permalink . '" title="' . $post_title.'" rel="nofollow">' . $post_title . '</a>';
                    echo $show_count == '1' ? ' ('.$post_count.')' : '';
                    echo $after;


        $options = get_option("dot_most_recommended_posts");
        if (!is_array( $options ))
            $options = array(
                'title' => 'Most recommended posts',
                'number' => '10',
                'show_count' => '1',

        $title = $options['title'];
        $numberOf = $options['number'];
        $show_count = $options['show_count'];

        echo '<ul class="mostrecommendedposts">';
            dot_most_recommended_posts($numberOf, '<li>', '</li>', $show_count);
        echo '</ul>';

The above code is abstracted from this plugin http://wordpress.org/plugins/i-recommend-this/.

Now the data displayed using above code is accurate. Based on the number of votes received a list of displayed. I want to know the best way to avoid using raw queries and using WP_Query instead.

I tried with the below code but it seems to display some only those posts which have a vote count of 9. Updated code with WP_Query - https://gist.github.com/hchouhan/6727363

$query_top_10_showcase = new WP_Query(
        'post_type' => array( 'showcase' ),
        'orderby' => 'meta_value',
        'meta_key' => '_recommended',
        'paged' => $paged

// Run the loop as normal
if ( $query_top_10_showcase->have_posts() ) :

    while ( $query_top_10_showcase->have_posts() ) : $query_top_10_showcase->the_post();

        $url = get_post_meta( $post->ID, '_dot_showcase_url', true );
        $recommended = get_post_meta( $post->ID, '_recommended', true );
    <?php the_title() ?> - <?php echo $recommended; ?>



// no posts found

share|improve this question
Please include the actual code in your question, rather than linking to it. – Johannes Pille Sep 27 '13 at 11:50
Hi. I initially had, but the code did not appear well and after few attempts simply moved it on gist. Would really appreciate if you could check it – Harish Chouhan Sep 27 '13 at 12:03
Thanks a lot @JohannesPille. – Harish Chouhan Sep 27 '13 at 12:47
up vote 0 down vote accepted

Finally found the answer.

Since I had not specified

            'orderby' => 'meta_value_num',
            'meta_key' => '_recommended',

The limit was being considered as count. I am not sure if am explaining this right

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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