0

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.

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
    Dec 5, 2018 at 14:59

1 Answer 1

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' );
1
  • Thanks I hadn't thought that because pre_get_posts is run before the UI is generated then I could just set $_GET. I had to add if !isset($_GET['orderby']) to stop it from totally overriding the other sortable columns. Dec 6, 2018 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.