我在wordpress帖子中有以下层次结构(例如):
-> Post 1 (pubdate: 2011-01-01)
--> Post 5 (pubdate: 2011-01-02)
---> Post 6 (pubdate: 2011-01-03)
----> Post 7 (pubdate: 2011-01-04)
-> Post 2 (pubdate: 2011-03-01)
--> Post 8 (pubdate: 2011-03-02)
-> Post 3 (pubdate: 2011-02-01)
--> Post 9 (pubdate: 2011-02-02)
---> Post 10 (pubdate: 2011-02-03)
-> Post 4 (pubdate: 2011-04-01)
我现在想要实现的是按照最新的子帖子的发布日期对父帖子进行排序。重要的是层次结构应该保持完整。这将导致示例中的以下顺序(从最新到最旧):
-> Post 4 (pubdate: 2011-04-01)
-> Post 2 (pubdate: 2011-03-01)
--> Post 8 (pubdate: 2011-03-02)
-> Post 3 (pubdate: 2011-02-01)
--> Post 9 (pubdate: 2011-02-02)
---> Post 10 (pubdate: 2011-02-03)
-> Post 1 (pubdate: 2011-01-01)
--> Post 5 (pubdate: 2011-01-02)
---> Post 6 (pubdate: 2011-01-03)
----> Post 7 (pubdate: 2011-01-04)
你知道我该怎么做吗?谢谢
EDIT: 我根据Rarst的问题添加了以下部分,即如何检索结构。
我通过以下查询检索帖子:
$args = array(
\'cat\' => 7,
\'post_type\' => \'articles\',
\'orderby\' => \'post_date\',
\'posts_per_page\' => -1,
\'order\' => \'DESC\'
);
$records = get_posts($args);
foreach($records as $post) {
get_template_part( \'index\', \'article\' );
}
然后我使用此函数检查一篇文章有多少个家长。。。
// count amount of parents of a post
function usr_count_children($cid) {
$childrenArray = count(get_pages("post_type=articles&child_of=".$cid));
return $childrenArray;
}
。。。并将返回的数字作为css类添加到文章的html元素中(然后根据该元素设置样式)。
EDIT 2:
好吧,这就是我到目前为止得到的:
$parentPosts = get_posts(array(
\'cat\' => 7,
\'post_type\' => \'articles\',
\'post_status\' => \'publish\',
\'post_parent\' => 0,
\'posts_per_page\' => -1,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\' ));
foreach ($parentPosts as $post){
echo "+ ".$post->post_title." ID: ".$post->ID."<br/>";
// Look up newest child (query, sort by date, limit to one)
$newestChild = get_pages(array(
\'child_of\' => $post->ID,
\'post_type\' => \'articles\',
\'post_status\' => \'publish\',
\'sort_column\' => \'post_date\',
\'sort_order\' => \'DESC\',
\'number\' => 1 ));
foreach($newestChild as $child){
echo "– ".$child->post_title." ID: ".$child->ID."<br/>";
}
}
我一直在使用第二个查询(获取最新的子项),除非我删除它,否则它不会返回任何子项
\'number\' => 1
. 我确实尝试了一些事情(比如使用WP\\u查询,我甚至尝试了原始的SQL查询),但我还是无法理解。任何人