我有一个自定义的帖子类型,设置如下:
$supports = array(
\'title\'
, \'editor\'
, \'thumbnail\'
, \'revisions\'
, \'page-attributes\'
);
$args = array(
\'hierarchical\' => true
, \'supports\' => $supports
[...]
);
register_post_type(\'myType\', $args);
我想显示所有帖子,并将它们像在wp管理区域一样进行排序(缩进是为了可读性):
1,
2,
3,
1, (parent 3)
2, (parent 3)
4
因此,我尝试了以下订单类型设置为“menu\\u order”的查询:
$loop = new WP_Query( array(
\'post_type\' => \'myType\'
, \'posts_per_page\' => 50
, \'orderby\' => \'menu_order\'
, \'order\' => \'ASC\'
));
不幸的是,所有帖子都是按
menu_order
, 但是
menu_order
独占,忽略父关系(
post_parent
属性)。所以我得到了这样的东西
1,
1, (parent 3)
2,
2, (parent 3)
3,
4
将查询更改为
\'orderby\' => \'parent menu_order\'
导致以下情况
1,
2,
3,
4
1, (parent 3)
2, (parent 3)
因此,在我看来,一切都按照设计进行,orderby值直接转换为相应的SQL“Order By”。
问题
获得所需订单的最简单方法是什么?
SQL我假设这是wordpress创建的主要SQL查询:
SELECT SQL_CALC_FOUND_ROWS wp_2_posts.ID
FROM wp_2_posts
WHERE 1=1 AND wp_2_posts.post_type = \'inhalt\' AND (wp_2_posts.post_status = \'publish\' OR wp_2_posts.post_status = \'private\')
ORDER BY wp_2_posts.post_parent, wp_2_posts.menu_order ASC LIMIT 0, 50
然后,接下来是:
SELECT wp_2_posts.*
FROM wp_2_posts
WHERE ID IN (40,42,44,46,48,50,52,54,56,58,60,76,62,65,69,71,74)
SELECT post_id, meta_key, meta_value
FROM wp_2_postmeta
WHERE post_id IN (40,42,44,46,48,50,52,54,56,58,60,62,65,74,69,71,76)
一个已知的解决方法,但不是一个答案,就是给所有帖子提供更高和更“间隔”的顺序值,如
100,
200,
300,
310,
320,
400