$args = array(\'orderby\' => \'date\',\'order\' => \'DESC\',\'post_type\' => \'products\', \'include\' => \'2, 4\');
// get the 2 and 4 posts
$children = get_posts($args);
// get the children of 2, and merge arrays
$children = array_merge($children,get_posts(array(\'orderby\' => \'date\',\'order\' => \'DESC\',\'post_type\' => \'products\',\'post_parent\'=>2)));
// get the children of 4, and merge arrays
$children = array_merge($children,get_posts(array(\'orderby\' => \'date\',\'order\' => \'DESC\',\'post_type\' => \'products\',\'post_parent\'=>4)));
foreach($children as $child){
// etc
}
尽管如果您可以在自定义分类法中标记它们,或者以其他方式识别它们,这样您只需要查找一件事,而不是三件事,那么会更容易/更快。
e、 g.如果我们有一个自定义分类法“突出显示的产品”,我们可能会:
$children = get_posts(\'post_type\' => \'products\', \'orderby\' => \'date\',\'order\' => \'DESC\',\'highlighted_products\' => \'example_page\');
foreach($children as $child){
// etc
}
这将更加灵活,更不容易出错(ID 2和4可能会更改!不要硬编码),而且执行速度更快,没有原始SQL或多个查询。更不用说,你现在在后端有一个很好的用户友好界面,你只需标记一个产品帖子,它就会出现在正确的位置