I have problem with sorting my custom post type in admin panel. Is it possible to leave children position below parent? I don't know if this is clear, but below I made examples trees.
My actual hierarchy without any sorting is:
Event 1
- AEvent 1
- BEvent 2
- CEvent 3
Devent 1
But, when I'm sorting by title (after click in column header) it look like:
- AEvent 1
- BEvent2
- CEvent 3
- DEvent1
- Event 1
And I would like it to look like this:
DEvent 1
Event 1
- AEvent 1
- BEvent 2
- CEvent 3
I'm trying to use this example, with simply modify with my CPT
// Use a query variable to control when to change the main admin query
add_filter( 'query_vars', 'custom_admin_list_query_vars', 10, 1 );
function custom_admin_list_query_vars( $vars ) {
array_push( $vars, 'custom_admin_list_children' );
return $vars;
}
add_action( 'pre_get_posts', 'custom_admin_pre_get_posts' );
function custom_admin_pre_get_posts( $query ) {
global $post_type;
// Change query only if it's a user-triggered query in admin
if ( ! is_admin()
|| 'events' != $post_type
|| $query->get( 'custom_admin_list_children' ) )
return false;
// Query only parents in date order
$query->set( 'post_parent', 0 );
$query->set( 'orderby', 'post_date' );
$query->set( 'order', 'desc' );
}
// Query the children of the parents above
add_action( 'wp', 'custom_admin_list_wp' );
function custom_admin_list_wp() {
global $post_type, $wp_query;
if ( ! is_admin() || 'events' != $post_type )
return false;
$args = array(
'post_type' => 'events',
'numberposts' => -1,
'custom_admin_list_children' => true,
'meta_key' => 'id',
'orderby' => 'meta_value_num',
'order' => 'asc'
);
// Get children
$children = array();
for( $i = 0; $i < count( $wp_query->posts ); $i++ ) {
$args['post_parent'] = $wp_query->posts[ $i ]->ID;
$children[ $i ] = get_children( $args );
echo '<pre>';
print_r($children);
echo '--';
print_r($wp_query->posts[ $i ]->ID);
echo '</pre>';
}
// Flag as a children with a '--' in front of the title
foreach( $children as &$c ) {
if ( !empty( $c->post_title ) )
$c->post_title = '— ' . $c->post_title;
}
// Put everything together
$posts = array();
for( $i = 0; $i < count( $wp_query->posts ); $i++ ) {
$posts[] = $wp_query->posts[ $i ];
$posts = array_merge( $posts, $children[ $i ] );
}
$wp_query->posts = $posts;
$wp_query->post_count = count( $posts );
}
But this not showing child posts.
question from:
https://stackoverflow.com/questions/65877949/wordpress-sorting-cpt-according-to-hierarchy-in-admin-panel 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…