admin管理员组

文章数量:1314471

all I want is to check if the custom post item in the loop has a parent. I am using this inside a shortcode. I could not find how to access the property from inside the loop.

Thank you my dears

$q = new WP_Query(array(
    'post_type' => 'my_custom_post_type',

    'posts_per_page' => 5,
    'category' => '',
));

while ($q->have_posts()) {
      $q->the_post();
      ... <- Check if the loop item has a parent
}
wp_reset_postdata();

all I want is to check if the custom post item in the loop has a parent. I am using this inside a shortcode. I could not find how to access the property from inside the loop.

Thank you my dears

$q = new WP_Query(array(
    'post_type' => 'my_custom_post_type',

    'posts_per_page' => 5,
    'category' => '',
));

while ($q->have_posts()) {
      $q->the_post();
      ... <- Check if the loop item has a parent
}
wp_reset_postdata();
Share Improve this question asked Nov 26, 2020 at 14:55 El GringoEl Gringo 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

The WP_Post class has an attribute post_parent that you can use for this.

while ($q->have_posts()) {
      $q->the_post();
      $post = get_post();
      if (!empty($post->post_parent)) {
          // do something different
      }
}

本文标签: custom post typesChecking if looped item has a parent inside a shortcode