[英]Wordpress order posts by custom meta in pagination
I'm ordering my posts by a custom meta value named "size". 我通过名为“ size”的自定义元值对帖子进行排序。
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'size'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->postmeta.meta_value DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
if ($pageposts):
global $post;
foreach ($pageposts as $post):
setup_postdata($post);
the_title();
endforeach;
endif;
wp_pagenavi(); //creates page navigation
At the same time I'm using the WP-pagenavi plugin to navigate the posts by pages. 同时,我使用WP-pagenavi插件逐页浏览帖子。 I have 10 posts on each page.
我每页有10个帖子。
The problem: Posts are ordered separately in each page. 问题:帖子在每个页面中单独订购。 How can I order posts in descending order through all pages?
如何在所有页面中按降序排列帖子?
Update: I might have found a solution but I'm not sure how to implement it in my code 更新:我可能已经找到了解决方案,但不确定如何在代码中实现
http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
$my_query = new WP_Query( array( 'tag' => 'foo', 'paged' => get_query_var('paged') ) );
while ( $my_query->have_posts() ) : $my_query->the_post();
the_title();
// more stuff here
endwhile;
wp_pagenavi( array( 'query' => $my_query ) );
wp_reset_postdata(); // avoid errors further down the page
I've found a SO post that can solve your problem : 我发现有一篇SO帖子可以解决您的问题:
How to sort a 'query_posts' function by custom field, while limiting posts by another custom field 如何通过自定义字段对“ query_posts”功能进行排序,同时通过另一个自定义字段来限制帖子
There you will find a custom class extending WP_Query
and allowing you to make a query ordered by a custom field, and to include the paged
query var too. 在那里,您将找到一个自定义类,该类扩展了
WP_Query
并允许您按自定义字段进行查询,并且还包括paged
查询var。
So the steps : 因此,步骤如下:
Past the class PostsOrderedByMetaQuery
code somewhere like in your functions.php
将
class PostsOrderedByMetaQuery
代码class PostsOrderedByMetaQuery
到类似于functions.php
某个位置
Replace your query by : 将查询替换为:
// Retrieve `paged` in URL $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // Make the query like in WP_Query but with our custom class $query = new PostsOrderedByMetaQuery(array( 'post_type' => 'post', 'post_status' => 'publish', 'paged' => $paged, 'orderby_meta_key' => 'size', 'orderby_order' => 'DESC' ));
Use it ! 用它 !
while ( $query->have_posts() ) : $query->the_post(); the_title(); // more stuff here endwhile; wp_pagenavi( array( 'query' => $query ) ); wp_reset_postdata(); // avoid errors further down the page
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.