hope this will usefull to some one.
<?php
/* ADDED by for sorting ASCENDING order but call for price (ie price with 0) last */
function my_sort_custom( $orderby, $query ){
global $wpdb;
$orderby = " case when CAST(meta_value AS SIGNED) = '0' then 1 else 0 end ,CAST(meta_value AS SIGNED) ";
return $orderby;
}
add_filter('posts_orderby','my_sort_custom',10,2);
global $wp_query;
$args = array_merge( $wp_query->query_vars, array('meta_key' => 'price'));
query_posts( $args );
?>
Related Posts:
- How to make orderby ‘meta_value_num’ OPTIONAL?
- Sort by price wpdb
- order by numeric value for meta value
- Ignoring initial articles (like ‘a’, ‘an’ or ‘the’) when sorting queries?
- Sorting: custom query with orderby meta_value_num THEN by title
- Reduce or prevent calling of update_meta_cache
- Sort posts by category name and title
- What is the most efficient way of querying posts based on visits and date for current day?
- WP_Query displaying ALL posts
- Loop through all tags & output posts in alphabetical list
- What is an efficient way to query based on post_meta?
- How to count post meta key values for all posts in database
- Sorting Posts by custom field
- How should I use posts_where to change meta_value from a string to integer?
- How can I create a WP_Query that returns posts where one meta_value
- Sorting meta_value as integer doesn’t work
- Sort in WP_Query(), not filter? Is it possible?
- How to get sum of meta_values of a meta_key in wp_query according to conditions
- Front End Sorting Questions
- Sort by meta key on archive page
- Best approach to create Hot and Trending sections
- How to sort posts in admin by titles with dd.mm.yyyy format?
- Order posts by meta value and Date
- Meta query with order by another custom field
- Query Posts depends on custom field inside repeater field using acf
- Get meta info related to current post
- Select from wp_post and multiple meta_value from wp_postmeta
- How do I sort posts with multiple pages
- Pull post meta with post_query?
- WP_query posts closest to todays date
- WP_Query posts by distance based on LAT & LNG in Database
- exclude posts with a specific custom field and value
- Changing WP_Query params with url Query Var
- How to make Meta Query case sensitive?
- Performance when getting post meta for post retrieved by meta value
- Get attachment by meta_key value
- Using hook to use DISTINCT in a wp_query
- Sort wordpress custom posts based on meta value
- WP_Query: Group events by year, sorted DESC; then by date for each year group, sorted ASC
- WordPress Comments sort by custom meta key
- Understanding the orderby in WP_Query?
- Where to put meta Keys
- WP_query sorting can’t sort danish letters (æ, ø, å)
- Large AND OR query timing out
- Help ordering Post loop by two meta values
- get_post_meta slowing down my page load (in a plugin)
- New WP_Query loop in admin causes problems
- How to sort a custom wordpress query by combination of meta values?
- Gallery shortcode numerical sorting
- get_post_meta in WP_Query
- Hide posts with meta key in WP_Query
- WordPress query posts with multiple post_meta data
- I am having problem sorting custom post type using WP_Query
- How do I check if an article is popular this week?
- Sorting search results with custom dropdown
- Woocommerce: order posts by meta key
- How do I sort this custom list of sticky posts
- Nested array issue in meta_query
- Display post meta conditionally based on it’s value
- Filtering by multiple conditions in the loop
- Sort by custom field that is an array?
- Display count number of posts with the same specific meta_key meta_value
- WP_Query meta compare must include ALL array values
- How to sort queried pages by an array of page ids?
- orderby ignored by wp_query
- Customize the Sorting Dropdown in WooCommerce use another list or more page
- wp query to use both author id and meta_query
- How to sort store location by specific category order in WP store locations
- How to sort wordpress posts already selected by WP_QUERY
- Meta query ignores multiple values of the key
- Trouble with serialized metadata
- How sort products by calculate value? ( custom post meta, price, option)
- Get authors list and sort them by recent posts
- Custom filter from post meta
- How to sort by multiple values in a nested WP_Query
- Query against multiple locations within single custom post type post
- WP Query with meta queries
- Case insensitive ORDERBY in wpquery
- Sort posts on custom field AND after that sort on date?
- How to mix two orderby-parameters into one ordered result
- update_post_meta() not updating
- I can’t get post based on its postmeta value and key
- Ignoring ‘a’ when sorting posts
- Saving custom fields for WP_Query to retrieve
- How to show all the associated posts with specific date of data metabox?
- Display three sequential posts on each page load, without repeating previous
- Sort by meta key within same day
- WP_Query Posts by Metadata from Option Tree
- Sorting is not working in WordPress WP_Query
- How to sort a WP_Query array by post_name after an array_merge();
- WP Query to order posts by multiple meta fields
- Custom Search Query – include only custom fields and title
- How to count post meta key values for all posts in database
- How to query post ids liked by the Author
- Sort Posts with custom meta key by default which is currently set as optional
- Query posts by meta value and sort by another meta key
- have_posts order by title descending
- WP_Query – order with usort by custom meta
- Sort by multiple columns using get_posts
- How to get posts that have certain meta key value and order based on another meta key’s value