admin管理员组文章数量:1130349
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 |1 Answer
Reset to default 0You 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' );
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_poststo identify when you are viewing that CPT, andis_admin()to make sure you are on the admin side, and then changeorderby. – WebElaine Commented Dec 5, 2018 at 14:59
1 Answer
Reset to default 0You 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' );
本文标签: custom post typesAdding orderby url parameter to main CPT admin menu link
版权声明:本文标题:custom post types - Adding orderby url parameter to main CPT admin menu link 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749126291a2319835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


pre_get_poststo identify when you are viewing that CPT, andis_admin()to make sure you are on the admin side, and then changeorderby. – WebElaine Commented Dec 5, 2018 at 14:59