Any tips on fitting pagination into my loop below and I owe you a beer or two. I've worked out how to pull the top rated articles by vote from a custom plugin, and lay it out with a count, but pagination has me stumped. I'm a self-taught learner here so any help would be greatly appreciated.

I was aiming for 6 posts shown per page then use pagination if needed. Two posts are shown and after that a new code section is inserted with a count, the count works, everything works except pagination.

Not sure how this fits in


when I already use


in my query...

<?php global $wpdb;
$query_sql = "SELECT like_pid FROM " . $wpdb->prefix ."likes_count ORDER BY like_count DESC LIMIT 6 OFFSET 0";
$query_result = $wpdb->get_col( $wpdb->prepare ($query_sql, OBJECT));
if ($query_result) {
foreach ($query_result as $post_id) {
$post = &get_post( $post_id );
setup_postdata($post); ?>

<?php $count++;
if ($count%2== 0) : ?>

<-- do the loop, count out two articles -->

<?php else : ?>

<-- do something after every two articles -->

<?php endif;?>

<?php } ?>
<?php } ?> //ugly but works

<div class="next"> <?php next_posts_link('&raquo;' ,0); ?></div>
<div class="previous"> <?php previous_posts_link('&laquo;' ,0); ?></div>

1 Answer 1


This should get the offset for you, and from there it's a simple matter of adjusting your query

$offset = "0";
$no_of_posts = the_posts_per_page( false ); //Number of posts to display on each page
if (preg_match('/page/', $_SERVER['REQUEST_URI'])) {
    $uri = explode('/', $_SERVER['REQUEST_URI']);
    foreach ($uri as $key=>$value) {
        if ($value == "") {
    $offset = (array_pop($uri) * $no_of_posts) - $no_of_posts;

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