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 validate XML-RPC post creation and cancel when needed?
- Custom Field in Featured image for A particular post
- Custom permalink structure only for default posts
- List latest posts in WP-Admin
- WordPress Number of Posts Not Changing With posts_per_page
- ascending order custom post type
- Comments enabled, but disabled at the same time
- set role specific screen options in post summary page
- How to get a nofication when post submitted
- Any idea why wp_insert_post is succeeding but not returning?
- add_action not using ‘delete_post’ action with wp_delete_post
- Getting post id from wp_insert_post_data function?
- Lazy Load using WP_Query pagination
- Is there any way to allow users to access content before it’s published?
- Restrict users post for himself
- Driving a random quote like functionality with database
- current post with current author
- Categories list into registration form
- Trying to alter the post_content through the_post
- How to add a class to each individual post?
- ACF to select posts not displaying on blog page
- How can I insert a new markup element after 4 posts automatically? [closed]
- Extract wordpress posts content and category content
- WordPress loop not working on static front page
- Sidebar on single.php not showing up
- Multiple Blog Pages
- Change modified date to current date when title updated automatically
- How to edit a custom theme?
- how to Update guid = “text”+post_name?
- SQL DELETE multiple post_meta on single and multi-sites
- How to put the author of the post in the comments?
- How to Best Create Separate Subscribers and User Experience
- Post not using taxonomy template
- How to display related post from same category in single.php
- How to get value of a selected option from select tag and use it in WP_query to filter posts?
- get current index of post content in category page
- Permalink structure for two seperate blogs
- wp_query args with relation
- How do I apply a wordpress function to something with no ID inside an archive page?
- Add new post only in assigned category
- How to change post template via url?
- How do I list categories and the common categories for posts beneath those categories?
- Assign a link of a post to that contains that post?
- wordpress pinboard theme [closed]
- How do I save JS classes being applied to the WP dashboard via custom plugin?
- I want to add category slug in posts url