$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - How to order WP_Query by parent for hierarchical Custom Post Type?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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

}
Post a comment

comment list (0)

  1. No comments so far