Replacing raw database query to WP_Query

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

Read More
        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_Queryhttps://gist.github.com/hchouhan/6727363

<?php
$query_top_10_showcase = new WP_Query(
    array(
        '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 );
?>
<p>
    <?php the_title() ?> - <?php echo $recommended; ?>
</p>

<?php
endwhile;

else:

// no posts found

endif;
wp_reset_postdata();
?>

Related posts

1 comment

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

Comments are closed.