Php 从自定义post类型获取同级

Php 从自定义post类型获取同级,php,wordpress,Php,Wordpress,我已使用以下结构设置了自定义帖子类型: 主岗 儿童邮政 兄弟姐妹职位 我有两个主要职位叫“Vrouwen”和“Mannen” 当我访问其中一个主要帖子时,我只想展示兄弟姐妹 我坚持要做到这一点 但随后也会显示“子对象”。 我需要更深一层 感谢您的帮助 我尝试了下面的代码 $mysibling = $post->post_parent; $mychild = $post->ID; $mychildmysibling = array( $myc

我已使用以下结构设置了自定义帖子类型:

  • 主岗
    • 儿童邮政
      • 兄弟姐妹职位
我有两个主要职位叫“Vrouwen”和“Mannen” 当我访问其中一个主要帖子时,我只想展示兄弟姐妹

我坚持要做到这一点

但随后也会显示“子对象”。 我需要更深一层

感谢您的帮助

我尝试了下面的代码

$mysibling        = $post->post_parent;
$mychild          = $post->ID;
$mychildmysibling = array( $mychild, $mysibling );

$args = array(
    'post_parent'    => $mychildmysibling,
    'post__not_in' => array( $post->ID ),
    'posts_per_page' => -1,
    'post_type'       => 'collectie'
);

$parent = new WP_Query( $args );
while ( $parent->have_posts() ) : $parent->the_post();

但随后也会显示“子对象”。我需要更深一层。

首先
post\u parent
需要一个数字,但您设置了一个数组。其次,您基本上需要使它只适用于主页?所以查询应该如下所示:

// find children for the main post
$children = get_children( array('post_parent' => $post->ID));

// check if the post has any children
if ( ! empty($children) ) {
   // get all posts where the parent is set as children to main post
   $args = array(
        'post_parent__in' => array_keys($children),
        'posts_per_page' => -1,
        'post_type' => 'collectie'
    );

   $siblings = new WP_Query( $args );

   if ( $siblings->have_posts() ) {
       while ( $siblings->have_posts() ) {
           $siblings->the_post();

           echo get_the_title();
       }
   }

   wp_reset_postdata();
}

谢谢你的回答。我越来越近了,但是孩子们也被展示出来了。我只需要得到兄弟姐妹。没关系!我犯了个错误!它工作了!非常感谢你