Thanks to @cameronjonesweb’s link, here’s the code you might use:
add_filter( 'manage_edit-post_sortable_columns', 'my_add_sortable_custom_column');
function my_add_sortable_custom_column( $columns ) {
$columns['views'] = 'views';
return $columns;
}
add_action( 'pre_get_posts', 'my_sort_custom_column' );
function my_sort_custom_column( $query ) {
if( ! is_admin() || ! $query->is_main_query() ) {
return;
}
if ( 'views' === $query->get( 'orderby') ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', 'views' );
$query->set( 'meta_type', 'numeric' );
}
}
The first one registers the column as sortable, while the second changes the query when WordPress wants to sort by that column.
Related Posts:
- How to Change Order of Posts in Admin?
- Get current menu_order
- Different post sort order within different categories
- what types of order can I pass to get_posts() via orderby?
- Sort posts by popularity/page views
- How do I reorder (pop and push) items from WP_Query?
- How to put last edited entry on top of the blog-post list?
- Sort post by attributes ‘order’
- How to sort posts by last name (2nd word) on ONE category only?
- Query posts from current year
- Reverse chronology of post listing
- Order Posts by Alphabetical for Each Letter
- Set number of article per number of page
- Problem ordering posts with numbers for titles numerically E.g. 1, 10, 100
- How to choose a sort order (for posts) per category? (ideally when creating a new category)
- Order post by year DESC and month ASC
- Display custom post types by date field
- orderby=meta_value_num creates duplicate posts
- Change Order of Admin Posts Depending on Meta
- Display posts in alphabetical order for a particular category
- post re-order on my site
- Display posts from #6 to #20 on archive page
- Why orderby=”date” does not support manually changed posts?
- WP_Query orderby not working with meta_value_num and menu_order
- How can I sort posts by the date and a custom meta field?
- Sort WP posts by span value
- How to sort posts inside categories
- Any way for get_next_post() to use the actual post order instead of publish order?
- Custom order for Mysql array
- How to order posts by meta_value and title
- Order posts alphabetically: how to set order=asc in mysql query?
- how to get post order by post id wp_query?
- How Do I Get My Post Tags to Alphabetize?
- While loop articles – if statement order
- Make a vertical dynamic list of posts in alphabetically order and in columns [closed]
- Create a list of posts with topic headdings
- How can I add a box to edit Order on a regular post?
- get_previous_post() while accounting for sticky posts
- Need to know custom code to display random and most viewed posts in wordpress posts and pages
- Posts and order
- Adjust the order for returned posts
- Order posts by date
- query posts with selected post ids first
- How do I display posts ordered by a date custom field?
- WP_Query sort by meta_value_num or date
- Allow reader to reorder posts for themselves (oldest first)
- Random order of the 5 most popular posts
- My custom query is displaying random posts in random order
- Get query result according to merged array
- How to make post button to a random post
- Where is object, property or label in the database that controls the order of the posts being displayed?
- Last three posts not being put in order by date. How to fix this?
- Order by ‘s’ using WP_Query()
- How to sort posts alphabetically based on a specific parent category
- How to get post creation date?
- Setting Custom Sort Order of Posts within a Category
- Display post number not post ID number
- How do I detach images from posts?
- Change slug with custom field
- Conditional that checks if a current category has any subcategory with posts
- Add code just after Post content
- Alternating post layout
- Check to see if specific loop has less than certain amount of posts
- Get current category id from post page wordpress
- Sticky post appears twice
- How to show last post of each website of a MU wordpress in HomePage
- Retrieve value between 2 post
- how to replace source ” https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css” in the wordpress backend?
- Editing the default page to show all posts, rather than most recent ones
- Start WP Pagination From Number 1 in URL
- How to make a bilingual front-end post using the plugin WP User Frontend and Polylang or qTranslate?
- Avoiding “Missed Schedule” errors when inserting future posts
- How to display two random-post sections that are each under their own category
- Tags on products vs tags on blog posts. An easy way to duplicate them
- Deleting post specific content at beginning of post
- Change Post Title For Specific Category
- Export all posts to import as product items
- WordPress as CMS: How to manage/handle images assigned to a post as full-sized background images in a slideshow?
- Prevent duplicate pages from being added
- Can I style single post that are in multiple catergories?
- Show 10 latest posts only from users with contributors role
- Mailing list submission from a post that doesn’t have a form on it
- Show single post child category from a determined parent
- Go back to the exact post page number
- Post data across WordPress sites
- The first post appears twice
- Find post tags words in post content and link them to tag URLs
- Check if user has avatar
- Hide title in Merlin Theme on posts and pages only
- is_archive() not working on selected “Posts page”
- How to associate media with a post?
- Why I obtain this WP_Query strange behavior trying to select posts having a specific tag?
- Apply comment to different post (not the current post) [duplicate]
- When Author add new post change default status to pending
- Adding custom fields to the Quick Edit screen – puzzled about the column concept
- fire action when post is being edited as well as saved?
- ‘posts_per_page’ => ’10’ does not show any post
- Page not found after clicking on pagination
- Enqueue CSS on custom posts’ backend area
- Edit Page button is gone