0

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 );

}
  • What do you want to do with these posts? It's hard to order posts in such way, because the structure is a tree, so you'll need a Walker to flatten that tree... – Krzysiek Dróżdż Feb 4 at 10:55
  • I need to display them in a loop and maybe pagination will be needed, something like archive page for each of those main posts. The structure is as it is, I can't change them to categories – Marko Feb 4 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.