admin管理员组

文章数量:1431720

I am trying to display the grandchildren of a custom post type.

The structure is is fairly simple:

Issues
   Issue 1
      Article 1
      Article 2
      Article 3 etc
   Issue 2
      Article 1
      Article 2
      Article 3 etc
   Issue 3 etc etc

Is there a way to display the Articles(grandchildren) in a single WP query specifying the ParentID?

    //displays all posts :(
    $args = array('post_type' => 'magazine',
        'child_of' => array(20321), //Issues (parent postID)
    );

I am trying to display the grandchildren of a custom post type.

The structure is is fairly simple:

Issues
   Issue 1
      Article 1
      Article 2
      Article 3 etc
   Issue 2
      Article 1
      Article 2
      Article 3 etc
   Issue 3 etc etc

Is there a way to display the Articles(grandchildren) in a single WP query specifying the ParentID?

    //displays all posts :(
    $args = array('post_type' => 'magazine',
        'child_of' => array(20321), //Issues (parent postID)
    );
Share Improve this question asked Apr 16, 2019 at 14:47 ianhmanianhman 135 bronze badges 3
  • Check this out: wordpress.stackexchange/questions/81645/… – Siddhesh Shirodkar Commented Apr 16, 2019 at 15:11
  • @SiddheshShirodkar that displays children and grandchildren. I only want to display the grandchildren. – ianhman Commented Apr 16, 2019 at 15:16
  • All you would have to do to make that grandchildren only is return $children before merging $children with $posts – mrben522 Commented Apr 16, 2019 at 16:14
Add a comment  | 

1 Answer 1

Reset to default 0

Solved my own question after further research and testing :) Ended up using 2 queries passing the result from one query to the other. Hope this helps someone.

    $argsIssues = array( 'post_type' => 'magazine',
        'post_parent__in' => array(20321), //get issue(children) posts from the issues(parent)
        'fields' => 'ids' //query only the postIDs
    );
    $q = get_posts( $argsIssues ); //run $argsIssues query

    $argsArticles = array( 'post_type' => 'magazine',
        'post_parent__in' => $q //get article(grandchildren) posts from issue(children) posts.
    );

    query_posts( $argsArticles ); //run $argsArticles query

    //while loop goes here to display posts from $argsArticles query

本文标签: wp queryHow to display grandchildren only of custom post