casualx
Member
Posted 3 years ago #
Hey, I was wondering if anyone could help me with this.
I am using a custom bit of code to organize my posts by custom field. The code works fine, but it is not paginating properly. The code is as follows:
<?php
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'acronym'
AND wposts.post_type = 'post'
ORDER BY wpostmeta.meta_value ASC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
<?php if ($pageposts): ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<!-- Post Stuff Goes Here -->
<?php endforeach; ?>
<?php endif; ?>
Any help would be appreciated. Thanks!
casualx
Member
Posted 3 years ago #
Found the solution here:
http://wordpress.org/support/topic/154300?replies=11
$total = "your custom query goes here, but without LIMIT and OFFSET, so the total number of posts that match the query can be counted";
$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 = "your query again, but with the LIMIT and OFFSET as follows: LIMIT $ppp OFFSET $offset";
$pageposts = $wpdb->get_results($wp_query->request, OBJECT);
albert lau
Member
Posted 2 years ago #
@casualx
Thank you very much for the solution.