通过DESC,ASC在自定义WP_Query中订购
-
-
好了,您应该发布WP_Query,以查看其格式是否正确.Well, you should post the WP_Query, to see if it is formed right.
- 0
- 2013-08-13
- Marin Bînzari
-
这就是荣耀.我目前未按任何顺序排序,但布尔元值b/c是使该级别或排序正常工作的唯一方法.该网站还使用"标题"帖子,优先于"精选"帖子.因此,我在此查询中排除了它们There it is in all it's glory. I'm currently not ordering by anything but that boolean meta value b/c it was the only way to get that level or sorting working. The site also uses 'headline' posts which take precedence over 'featured' posts. Thus I exclude them in this query
- 0
- 2013-08-13
- ian
-
1 个回答
- 投票数
-
- 2013-08-13
尝试一下:
$args = array( 'post_type' => 'post', 'meta_key' => 'pb_issue_featured', 'orderby' => 'meta_value', 'order' => 'DESC', 'posts_per_page' => $posts, 'paged' => $paged, 'paged' => 1, 'meta_query' => array( array( 'key' => 'headline', 'value' => 1, 'compare' => '!=' ) ) ); add_filter( 'posts_orderby', 'filter_query' ); $q = new WP_Query($args); remove_filter( 'posts_orderby', 'filter_query' ); function filter_query( $query ) { $query .= ', wp_posts.menu_order ASC'; return $query; }
Try this:
$args = array( 'post_type' => 'post', 'meta_key' => 'pb_issue_featured', 'orderby' => 'meta_value', 'order' => 'DESC', 'posts_per_page' => $posts, 'paged' => $paged, 'paged' => 1, 'meta_query' => array( array( 'key' => 'headline', 'value' => 1, 'compare' => '!=' ) ) ); add_filter( 'posts_orderby', 'filter_query' ); $q = new WP_Query($args); remove_filter( 'posts_orderby', 'filter_query' ); function filter_query( $query ) { $query .= ', wp_posts.menu_order ASC'; return $query; }
我需要在查询中进行多级排序.问题在于如何像在SQL中那样对一个值DESC和另一个ASC进行排序.在终端中运行以下SQL似乎可以满足我的要求:
pb_issue_featured
是一个布尔值.我需要的最终结果是查询以在顶部显示此字段的值为1的帖子,然后显示所有其他信息.然后第二层订购是指定的menu_order
(我正在使用帖子类型订购插件).问题是我的布尔值需要从高到低(从1到0)排序,但是menu_order相反.插件首先订购的菜单的菜单顺序为1.因此,无法使用WP_Query中内置的" orderby".有人有建议吗?我调查了"posts_orderby"过滤器,但无法理解.真的不确定应该在哪里应用它或如何解决它.它只是没有按照我的方式重新排序.
感谢您的帮助!如果相关,我将发布实际的WP_Query,但我想使其尽可能短.
查询参数: