You can use pre_get_posts
with a callback:
<?php
defined( 'ABSPATH' ) OR exit;
/** Plugin Name: (#102854) Order Posts by Foo */
add_filter( 'pre_get_posts', 'wpse_102854_orderby_foo' );
function wpse_102854_orderby_foo( $query )
{
if (
! $query->is_main_query()
OR ! is_admin()
OR 'edit.php?post_type=YOUR_POST_TYPE' !== $GLOBALS['parent_file']
// Other conditions that force an abort, Example:
// OR 'foo' !== $query->get( 'some_query_var' )
)
return $query;
$query->set( 'orderby', 'meta_value_num' );
// etc.
return $query;
}
Related Posts:
- Nested meta_query with multiple relation keys
- Meta query terribly slow
- meta_query sorting by 2 keys
- How to handle optional end date in compare clause in meta_query
- Using meta_query on pre_get_posts to exclude a particular meta_key value
- Query between two meta keys
- How to check a single number value against an array of meta_key values?
- Meta query with multiple logic (AND / OR)
- Different page parameters inside query
- meta_query key value from array
- Is there a way to do multiple ordering on a multiple meta_query?
- LIKE %…% Meta Query
- Multiple Values stored as array in Meta Query
- Order by nested named meta queries with ‘exists’ and ‘not exists’
- Documentation for post meta
- Arrange Posts By Date In Order Of Closest To The Current Date
- pre_get_posts filter using numeric meta_query comparison (from dates)
- Custom Upcoming Events List for Events Manager Plugin
- Very slow query with meta_query on large database
- How to use meta_query to check if a meta field has something set?
- User Relationship
- How to make a WP REST API query with meta_query in WP4.7?
- Edit post_content on posts with Featured Image
- Nested Queries using meta_query
- Use both meta query and tax query
- meta_query, number comparison, not quite working as it should
- Custom query with DECIMAL(5,2)
- Query posts only if meta_value is not empty
- How to pass user meta_key and meta_value (values as array)
- WP_Query ordered by meta_value_num doesn’t fetch posts without this meta field
- Query Multiple values for same key
- Comparing a field with several values at once with meta_query
- Meta query with a sub query group possible?
- meta_query compare=’!=’ with multiple custom fields
- Query posts with numeric meta values within a given range
- WP 3.1 meta_query for multiple custom field values
- Meta query for custom post type ignored in main query
- Fastest way to do meta query when I don’t need the actual posts, and just need post_id?
- meta_query not working on live site
- How do you get Posts by multiple meta_keys and meta_values with the Rest API V2?
- Query custom field with date
- Meta query with regular expression does not work
- Searching in title or meta, with a tax query too!
- Pre Get Posts / Multiple Meta Keys / Orderby Single Key
- Help on multiple meta_query
- Meta query – How to display a null meta key
- pre_get_posts and set
- WordPress Screwing Up ‘orderby’ => ‘meta_value_num’ in ‘pre_get_posts’
- How to query wp_postmeta table for any specific meta_value?
- User meta query results in PHP notice: only variables should be passed by reference
- meta_query with multiple values optimization
- Get a single post ID based on an exact match of 1 or more meta values
- How to use orderby with meta_query?
- meta_query with multiple key
- last_name + first_name orderby with meta_query [solved]
- meta query based on month regardless of year
- Extend meta query arguments
- get_posts( $args ) return empty data
- meta_query in WP_User_Query not working – returns every user, not just users where key & value matches
- how to get post meta where value is an array of key value pairs
- meta_query array in ‘values’ returns an error
- Meta Query Based on Month Range
- how to search all user meta data that have value like “vivek”
- Find posts by related taxonomy field
- wordpress meta value compare between two date
- WP JSON API meta_query not working
- Ordering custom posts by meta field date
- meta_query with array as value with multiple arrays
- How to properly escape values in meta query
- Fetch custom post related to a User
- How to Ordering by user meta
- meta_query compare “=“ returns nothing when it seems it should
- Order resultset by configured value and then list all the rest
- How to send variable to wp meta_query value?
- Querying terms with calculations based on term meta data, sql vs. get_terms
- get_user can’t read variable
- meta_query check multiple date formats
- WordPress meta query not working
- Get meta_value from GDRating
- Why does this incorrect pre_get_posts meta_query work for sort order?
- Meta_Query with multi select values
- How to sort custom posts including null meta-values in admin columns with meta_query?
- Get Posts from Custom Meta Query
- get posts meta query returning the latest post if no meta key found
- Search using specific meta fields only (excluding post title and content)
- How to fetch WP_User_Query with multiple role arguments [duplicate]
- How to get meta value based on latest post id with group by term name
- Meta query not returning results even though the sql contains the information
- Display Users from two roles in one list
- Multiple Queries with meta_query
- Get foreach for meta_query value
- meta_query and strange orderby behaviour
- Sorting not working with get_posts
- order by meta_key [duplicate]
- Meta query with string NOT ending like pattern
- ACF meta_query field treated as “0” resulting in false negative search when testing ranges
- acf/save_post affecting WP_Query results
- Merge meta_value’s into a single row and then loop
- Is it possible to customize meta query in this way?
- How making a new search box in admin post table working on a specific column