$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'); ?>custom post types - Adding orderby url parameter to main CPT admin menu link|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)

custom post types - Adding orderby url parameter to main CPT admin menu link

matteradmin9PV0评论

Is it possible to change the main admin menu link for a CPT (the link to edit.php) to include URL parameters?

I wan't to make the posts list default to sorting by "title" but I don't like how updating the main query to force a "default" order doesn't set column header to reflect it.

If you click on the "Title" column header it adds the "orderby" url parameter so I'm wondering if there is a hook/filter that would allow me to append this to the menu link.

I can't see how this could be done when registering the new post type and would prefer not to use javascript to add it on after the page has loaded.

Is it possible to change the main admin menu link for a CPT (the link to edit.php) to include URL parameters?

I wan't to make the posts list default to sorting by "title" but I don't like how updating the main query to force a "default" order doesn't set column header to reflect it.

If you click on the "Title" column header it adds the "orderby" url parameter so I'm wondering if there is a hook/filter that would allow me to append this to the menu link.

I can't see how this could be done when registering the new post type and would prefer not to use javascript to add it on after the page has loaded.

Share Improve this question asked Dec 5, 2018 at 10:36 Dale DaviesDale Davies 33 bronze badges 1
  • You could probably use pre_get_posts to identify when you are viewing that CPT, and is_admin() to make sure you are on the admin side, and then change orderby. – WebElaine Commented Dec 5, 2018 at 14:59
Add a comment  | 

1 Answer 1

Reset to default 0

You can set $_GET directly inside your pre_get_posts action to get the UI to pickup that change:

function wpd_test_pre_get( $query ) {
    // put whatever conditions to target your cpt here
    if( is_admin() && $query->is_main_query() ){
        // modify query
        $query->set('orderby', 'title');
        $query->set('order', 'asc');
        // set $_GET vars
        $_GET['orderby'] = 'title';
        $_GET['order'] = 'asc';
    }
}
add_action( 'pre_get_posts', 'wpd_test_pre_get' );
Post a comment

comment list (0)

  1. No comments so far