1
votes

Is this possible to sort WP_Query results by post meta if the meta looks like this:

meta_key: _file_details

meta_value: { "file_sort_order": "2", "file_description": "aaa", "file_enabled": "true", }

As a result I need be able to get custom posts by post type, sorted by post meta value 'file_sort_order'.

1

1 Answers

2
votes

From the codex at WP_Query's page:

'meta_value' - Note that a 'meta_key=keyname' must also be present in the query. Note also that the sorting will be alphabetical which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect). Use 'meta_value_num' instead for numeric values. 'meta_value_num' - Order by numeric meta value (available with Version 2.8). Also note that a 'meta_key=keyname' must also be present in the query. This value allows for numerical sorting as noted above in 'meta_value'.

 $my_query = new WP_Query( array( 
                        // 'post_type' => 'post_type_name',
                        'meta_key' => 'key_name',
                        'orderby' => 'meta_value_num'
                        )); 

You can check out the Orderby Parameters on that same link.