Help learner understanding a query
-
I’m trying to figure a custom loop out from here so I can use pagination but unsure what I’ve done wrong. I’m trying to pull the top rated posts rom a database table and order them by votes.
Is my sql wrong? it works in another loop, but one that does not paginate. Thanks for any advice.
<?php global $wpdb; $total = "SELECT like_pid FROM " . $wpdb->prefix ."likes_count ORDER BY like_count"; $totalposts = $wpdb->get_results($total, OBJECT); $ppp = intval(get_query_var('posts_per_page')); $wp_query->found_posts = count($totalposts); $wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp); $on_page = intval(get_query_var('paged')); if($on_page == 0){ $on_page = 1; } $offset = ($on_page-1) * $ppp; $wp_query->request = "SELECT like_pid FROM " . $wpdb->prefix ."likes_count ORDER BY like_count DESC LIMIT $ppp OFFSET $offset"; $pageposts = $wpdb->get_results($wp_query->request, OBJECT);?> <?php if ($pageposts): ?> <?php foreach ($pageposts as $post): ?> <?php setup_postdata($post); ?> <h1><?php the_title(); ?></h1> <?php endforeach; ?> <?php else : ?> <p>No matching entries found.</p> <?php endif; ?>
Viewing 9 replies - 1 through 9 (of 9 total)
Viewing 9 replies - 1 through 9 (of 9 total)
- The topic ‘Help learner understanding a query’ is closed to new replies.