How to order WP_Query by parent for hierarchical Custom Post Type?

How can I get all items under specific parent and order them by menu order in hierarchical Custom Post Type? Lets say I have this posts structure:

  • Fruits
    • Apples
    • Bananas
    • Plum
    • Cherry
  • Vegetables
    • Potatoes
    • Beans
    • Beet
  • Nuts
    • Walnut
    • Chia seeds

This doesn't work:

$args = array(
    'post_type'      = 'vile',
    'posts_per_page' = -1,
    'post_parent__in' = array(543), // Only children of Fruits or Vegetables or Nuts
    'order'          = 'ASC',
    'orderby'        = 'menu_order'
);
$query = new WP_Query($args); 

Registering CPT

add_action( 'init', 'register_vile', 0 );
function register_vile() {

    $labels = array(
        'name' = _x( 'Vile', 'Vile', 'mytextdomain' ),
    );
    $args   = array(
        'label'               = __( 'Vile', 'mytextdomain' ),
        'labels'              = $labels,
        'supports'            = array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
        'taxonomies'          = array( 'post_tag' ),
        'hierarchical'        = true,
        'public'              = true,
        'show_ui'             = true,
        'show_in_menu'        = true,
        'menu_position'       = 5,
        'show_in_admin_bar'   = true,
        'show_in_nav_menus'   = true,
        'can_export'          = true,
        'has_archive'         = true,
        'exclude_from_search' = true,
        'publicly_queryable'  = true,
        'capability_type'     = 'page',
        'rewrite'             = array(
            'slug' = 'vile',
        ),
    );
    register_post_type( 'vile', $args );

}

Topic menu-order wp-query custom-post-types Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.