分页Wordpress $ wpdb查询吗? [英] Paginate Wordpress $wpdb Query?
本文介绍了分页Wordpress $ wpdb查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个查询:
<?php
$query= "SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'votes' AND wposts.post_status = 'publish' AND wposts.post_type = 'post' ORDER BY CAST(wpostmeta.meta_value AS SIGNED) DESC LIMIT 10";
$posts = $wpdb->get_results($query, OBJECT);
if ($posts ) : foreach ($posts as $post):
setup_postdata($post);
?>
// Post here
<?php endforeach; endif; ?>
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
我之所以使用它,是因为Wordpress无法正确排序使用数字的meta_values ...一切正常,除非我不知道如何使用wp_pagenavi进行分页.
I'm using this because Wordpress can't properly order meta_values that uses numbers, anyway...everything works fine except I have no clue on how to paginate this using wp_pagenavi.
有什么主意吗?
推荐答案
尽管我不建议这样做,但是您可以尝试更改全局$wp_query
对象的属性.
Although I wouldn't recommend it, you could try changing the properties of the global $wp_query
object.
global $wp_query; // shouldn't be required
$query = "SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = 'votes'
AND wposts.post_status = 'publish' AND wposts.post_type = 'post' ORDER BY
CAST(wpostmeta.meta_value AS SIGNED) DESC LIMIT 10";
$posts = $wpdb->get_results($query, OBJECT);
$wp_query->posts = $posts;
$wp_query->is_paged = true;
$wp_query->current_post = -1;
// etc etc
您可以查找 var_dump()或
You can look up the definition of the WP_Query class or do a var_dump() or print_r() on the $wp_query object after calling query_posts.
祝你好运!
这篇关于分页Wordpress $ wpdb查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文