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
- How to only display posts whose meta_value field is not empty?
- Can wp_query return posts meta in a single request?
- order by numeric value for meta value
- Ignoring initial articles (like ‘a’, ‘an’ or ‘the’) when sorting queries?
- How do I query for posts by partial meta key?
- Use REGEXP in WP_Query meta_query key
- WordPress retrieving meta data for all custom post types in list view
- Order by optional meta key?
- Sorting: custom query with orderby meta_value_num THEN by title
- meta_query: using BETWEEN with floats and/or casting to DECIMAL
- Reduce or prevent calling of update_meta_cache
- Is there a way to extend WP_query so Custom Post Types can have properties?
- Sort posts by category name and title
- How to get order of posts?
- What is the most efficient way of querying posts based on visits and date for current day?
- How to get Page/Post Gallery attachment images in order they are set in backend using WP_Query()?
- WP Query orderby meta key natural sort?
- Getting attachments by meta value
- WP_Query displaying ALL posts
- meta_query where value is equal to given value
- Query Custom Meta Value with Increment
- WP-CLI How to generate a list of posts with corresponding meta values
- Loop through all tags & output posts in alphabetical list
- What is an efficient way to query based on post_meta?
- WP_Query not working as expected for attachments and custom meta_query
- How to count post meta key values for all posts in database
- Sorting Posts by custom field
- Group posts by meta_key
- 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
- WordPress altering my custom query, How to fix it?
- WP Meta Query for some meta (array) values
- WP Query post meta value
- Sort in WP_Query(), not filter? Is it possible?
- Would this post meta be better added to the post table rather than post_meta table
- Custom URl parameter
- query posts in functions.php and update a field
- Compile meta values from custom loop into array and then calculate sum total
- How to get sum of meta_values of a meta_key in wp_query according to conditions
- how to fire join query with post_meta
- Front End Sorting Questions
- Woocommerce custom loop to show all the products [closed]
- Sort by meta key on archive page
- Best approach to create Hot and Trending sections
- WP_Query, custom sort and custom filter
- How to sort posts in admin by titles with dd.mm.yyyy format?
- Query Set Order By Author
- WordPress: Keep order of query_posts list of post ID’s using post__in
- Order posts by meta value and Date
- Efficient way to update multiple post meta
- Meta query with order by another custom field
- Query Posts depends on custom field inside repeater field using acf
- Query post with meta_query where date is not in future
- Minimising Database Queries when using Advanced Custom Fields
- Get meta info related to current post
- How to orderby meta_value_num with dollar ($) sign
- 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?
- Group WP_Query by meta_key date
- 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
- Inserting serialized value into wp_postmeta using update_post_meta
- How to make Meta Query case sensitive?
- Performance when getting post meta for post retrieved by meta value
- Get attachment by meta_key value
- Display custom field meta outside loop, site wide
- Using hook to use DISTINCT in a wp_query
- Fetch Record based on meta key dates
- 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
- how to make members list directory through wordpress post custom meta key.
- WP_query sorting can’t sort danish letters (æ, ø, å)
- How do I query for posts by partial meta key?
- 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?
- echo a specific meta_key queried through a custom post
- Gallery shortcode numerical sorting
- How to increase load time of an archive/search page (WP_Query)
- WP Group posts by year(desc) > month(desc) > date(asc)
- get_post_meta in WP_Query
- Which is faster wpdb & get_row or wp_query & ge_post_meta?
- Does wp track views for posts?
- Hide posts with meta key in WP_Query
- Sort WordPress Posts Meta value by Week not Day
- Filter posts by comparing custom meta value against postdate
- WordPress query posts with multiple post_meta data
- How to order WP_Query to group results?
- WP_Query – Accessing MetaValue from Query Result