Custom order of CPT posts by title, in wp-admin area
Question
I have a custom post type called “careers”.
<?php
/**
* Careers Post Type
*/
function _mchild_careers_post_type() {
$labels = array(
'name' => _x( 'Careers', 'Post Type General Name', '_mchild' ),
'singular_name' => _x( 'Career', 'Post Type Singular Name', '_mchild' ),
'menu_name' => __( 'Careers', '_mchild' ),
'name_admin_bar' => __( 'Careers', '_mchild' ),
'parent_item_colon' => __( 'Parent Career:', '_mchild' ),
'all_items' => __( 'All Careers', '_mchild' ),
'add_new_item' => __( 'Add New Career', '_mchild' ),
'add_new' => __( 'Add New', '_mchild' ),
'new_item' => __( 'New Career', '_mchild' ),
'edit_item' => __( 'Edit Career', '_mchild' ),
'update_item' => __( 'Update Career', '_mchild' ),
'view_item' => __( 'View Career', '_mchild' ),
'search_items' => __( 'Search Careers', '_mchild' ),
'not_found' => __( 'None Found', '_mchild' ),
'not_found_in_trash' => __( 'None Found in Trash', '_mchild' ),
);
$args = array(
'label' => __( 'Careers', '_mchild' ),
'description' => __( 'Careers', '_mchild' ),
'labels' => $labels,
'supports' => array( 'title', 'editor','thumbnail', 'author', 'page-attributes', 'excerpt', 'revisions' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-site-alt3',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'rewrite' => array('slug' => 'career', 'with_front' => true, 'hierarchical' => true),
'query_var' => true,
);
register_post_type( 'careers', $args );
}
add_action( 'init', '_mchild_careers_post_type', 0 );
By default, I would like to order all the listed posts under “careers”, in the wp-admin area, by title in either asc or desc order. What is the modern way of doing this? I tried the following but it didn’t work….
function set_post_order_in_admin($wp_query)
{
global $pagenow;
if (is_admin() && 'edit.php?post_type=careers' == $pagenow && !isset($_GET['orderby'])) {
$wp_query->set('orderby', 'title');
$wp_query->set('order', 'ASC');
}
}
add_filter('pre_get_posts', 'set_post_order_in_admin', 5);
I also tried the following but no luck…
function posts_for_current_author($query) {
if($query->is_admin) {
if ($query->get('post_type') == 'careers')
{
$query->set('orderby', 'title');
$query->set('order', 'ASC');
}
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
0
2 months
2022-12-08T22:13:37-05:00
2022-12-08T22:13:37-05:00 0 Answers
0 views
0
Leave an answer