Title: orderby=meta_value_num and users posts ERRORS
Last modified: August 30, 2016

---

# orderby=meta_value_num and users posts ERRORS

 *  [mkiisoft](https://wordpress.org/support/users/mkiisoft/)
 * (@mkiisoft)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/)
 * Hello everyone! Me again (?) lol
 * I’m facing two issues:
 * -I can’t make it work orderby=meta_value_num with my meta key. I have a custom
   webservice using “query_posts” default from WordPress like thing “query_posts(‘
   meta_key=post_views_count&category_name=mapas&orderby=meta_value_num&order=DESC&
   posts_per_page=-1’ . ‘&paged=’ . $paged);”
 * And I need to recreate the exactly same thing with WP REST API. There I’m using
   a custom meta field (“post_view_counts”) and I’m order DESC, orderby=meta_value_num
   to get the most view post first. Any help using this REST?
 * -Issue num 2: Now I can see the users information (I’ve comment the lines asking
   for access)… but I can’t see posts from each user.
 * The end point “[http://mywebsite.com/wp-json/users/1/posts&#8221](http://mywebsite.com/wp-json/users/1/posts&#8221);
   is giving me “[{“code”:”json_no_route”,”message”:”No route was found matching
   the URL and request method”}]”
 * Any fix for that???
 * Thanks a lot! Hope I can fix this ASAP
 * [https://wordpress.org/plugins/json-rest-api/](https://wordpress.org/plugins/json-rest-api/)

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Thread Starter [mkiisoft](https://wordpress.org/support/users/mkiisoft/)
 * (@mkiisoft)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/#post-6288742)
 * I have found a solution for meta_value_num end point:
 * yourwebsite.com/wp-json/posts?filter[meta_key]=YOURMETAKEY&filter[order]=DESC&
   filter[orderby]=meta_value_num
 * (Replace YOURMETAKEY with your actual custom field)
 * add %20date (at the end of meta_value_num without “&” or anything else) if also
   want to orderby date in addition of your query.
 * In order to get your custom meta key use Advance Custom Fields (if you want. 
   Could be other or manually). Download the plugin: “Code Snippets” and add the
   next Snippet:
 * function slug_allow_meta( $valid_vars ) {
    $valid_vars = array_merge( $valid_vars,
   array( ‘meta_key’, ‘meta_value’ ) ); return $valid_vars; } add_filter( ‘json_query_vars’,‘
   slug_allow_meta’ );
 * This will allows you to use “filter[meta_key]” and “filter[meta_value]” on your
   end points.
 * If someone could help me with the users posts… 🙂
 * Thanks and hope my answer helps someone.
 *  [Ajay M Aravind](https://wordpress.org/support/users/ajay-aravind/)
 * (@ajay-aravind)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/#post-6288827)
 * Hei,
 * Could you share me an idea to filter based on a number range.
    i.e, meta_value
   between ‘lower limit’ and ‘upper limit’?
 *  Thread Starter [mkiisoft](https://wordpress.org/support/users/mkiisoft/)
 * (@mkiisoft)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/#post-6288876)
 * Hello Ajay! I have come with only a partial solution… I can only compare “>, 
   >=, =, <, <=” from one value like this:
 * yourwebsite.com/wp-json/posts?filter[meta_query]&filter[meta_key]=post_views_count&
   filter[meta_value]=1800&filter[meta_compare]=%3C&filter[order]=DESC&filter[orderby]
   =meta_value_num&filter[category_name]=maps
 * That would give you the category “maps” order by number, in order DESC, every
   single value (stock query is 10 responds) “less than” (%3C is “>”) 1800 “views”.
   Of corse you can change the category, the order and the meta key with your custom
   key.
 * On a meta_compare the most usual usage would be: 1st value “>=” and 2nd value“
   <=” but I thing that BETWEEN would be better for that.
 * I’m sure that the compare BETWEEN most be really close to my answer. I’m keep
   working on it.
 * The Snippet you need is this one:
 * function slug_allow_meta( $valid_vars ) {
    $valid_vars = array_merge( $valid_vars,
   array( ‘meta_key’, ‘meta_value’, ‘meta_query’, ‘meta_compare’ ) ); return $valid_vars;}
   add_filter( ‘json_query_vars’, ‘slug_allow_meta’ );
 * Hope that works for you!
 *  Thread Starter [mkiisoft](https://wordpress.org/support/users/mkiisoft/)
 * (@mkiisoft)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/#post-6288901)
 * Here is a way to compare 2 values:
 * yourwebsite.com/wp-json/posts?filter[meta_query]&filter[meta_key]=your_custom_key&
   filter[meta_value]=600,999&filter[meta_compare]=BETWEEN&filter[orderby]=meta_value_num&
   filter[order]=DESC&filter[category_name]=your_category
 * The only problem I’m facing is that I can’t use values greater than 999 to copare
   using BETWEEN and that is a problem. I can use floating point and also works,
   example: 4.1, 5.6 and it works.
 * If someone knows how to fix the 999 limitation, it would be amazing! AMAZING!
 * EDIT: The issue is always to get values grater than 999… if you put 1800 “less
   than” it works (????) but 1800 “greater than” it doesn’t. Makes no sense!
 * EDIT 2: Looks like that happend with “strings”… in my case views are parsed as
   strings. I’ll check using intval to parse the value as int and then compare those
   2 values.
 *  [Ajay M Aravind](https://wordpress.org/support/users/ajay-aravind/)
 * (@ajay-aravind)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/#post-6288940)
 * Great work mkiisoft. Thanks for the help. will test all these and try to improve
   it..
 *  [Ajay M Aravind](https://wordpress.org/support/users/ajay-aravind/)
 * (@ajay-aravind)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/#post-6288943)
 * Your code works for me with same issue. I need meta value as float (in certain
   cases) and those are not working.
 *  [Ajay M Aravind](https://wordpress.org/support/users/ajay-aravind/)
 * (@ajay-aravind)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/#post-6288945)
 * An update:
 * we could use meta_type to specify the type of meta_value
 * function slug_allow_meta( $valid_vars ) {
 *     ```
       $valid_vars = array_merge( $valid_vars, array( 'meta_key','meta_value', 'meta_query', 'meta_compare','meta_type' ) );
       return $valid_vars;
       }
       add_filter( 'json_query_vars', 'slug_allow_meta' );
       ```
   
 * query will be:
 * yourwebsite.com/wp-json/posts?filter[meta_query]&filter[meta_key]=your_custom_key&
   filter[meta_value]=600,999&filter[meta_compare]=BETWEEN&&filter[meta_type]=DECIMAL&
   filter[orderby]=meta_value_num&filter[order]=DESC&filter[category_name]=your_category
 *  [Ajay M Aravind](https://wordpress.org/support/users/ajay-aravind/)
 * (@ajay-aravind)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/#post-6288946)
 * Next thing I am trying is fetching with more than one meta keys. If someone could
   help, it would be great. Hope this thread will be good documentation for others..
 *  Thread Starter [mkiisoft](https://wordpress.org/support/users/mkiisoft/)
 * (@mkiisoft)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/#post-6288980)
 * Typo: “%3C” is “<” less then (on: [here](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors?replies=9#post-7138145))
 * Ajay, your snippet works amazing… did you try using &filter[meta_query] again?
   meta_query is an array, so on…?
 * I made a few tests but not good luck so far.
 * I have dozens of end points right now. It would be great to make a full post 
   with all of them from everyone 🙂
 *  Thread Starter [mkiisoft](https://wordpress.org/support/users/mkiisoft/)
 * (@mkiisoft)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/#post-6288984)
 * Update: Nested meta query are available since WP 4.1 like [THIS (documentation)](https://make.wordpress.org/core/2014/10/20/update-on-query-improvements-in-4-1/):
 * $query = new WP_Query( array(
    ‘meta_query’ => array( ‘relation’ => ‘OR’, array(‘
   relation’ => ‘AND’, array( ‘key’ => ‘city’, ‘value’ => ‘Miami’, ), array( ‘key’
   => ‘state’, ‘value’ => ‘Ohio’, ),
 *  ),
    array( ‘relation’ => ‘AND’, array( ‘key’ => ‘city’, ‘value’ => ‘Augusta’,),
   array( ‘key’ => ‘state’, ‘value’ => ‘Maine’, ),
 *  ),
    ), ) );
 * And it looks like we need to add “relation” (meta_relation) to have OR and AND
   values and do that.
 * I can’t test it right know but it would be something like this:
 * yourwebsite.com/wp-json/posts?filter[meta_query]&filter[meta_relation]=OR&filter[
   meta_relation]=AND&filter[meta_key]=your_custom_key&filter[meta_value]=600,999&
   filter[meta_compare]=BETWEEN&filter[meta_type]=DECIMAL&filter[meta_key]=your_second_custom_key&
   filter[meta_value]=4.0,5.5&filter[meta_compare]=BETWEEN&filter[meta_type]=DECIMAL&
   filter[orderby]=meta_value_num&filter[order]=DESC&filter[category_name]=your_category
 * I’ll try all the different methods later today or tomorrow.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘orderby=meta_value_num and users posts ERRORS’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/json-rest-api_2e3641.svg)
 * [WP REST API (WP API)](https://wordpress.org/plugins/json-rest-api/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/json-rest-api/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/json-rest-api/)
 * [Active Topics](https://wordpress.org/support/plugin/json-rest-api/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/json-rest-api/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/json-rest-api/reviews/)

## Tags

 * [api](https://wordpress.org/support/topic-tag/api/)
 * [meta value](https://wordpress.org/support/topic-tag/meta-value/)
 * [orderby](https://wordpress.org/support/topic-tag/orderby/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [users](https://wordpress.org/support/topic-tag/users/)
 * [wp-rest](https://wordpress.org/support/topic-tag/wp-rest/)

 * 10 replies
 * 2 participants
 * Last reply from: [mkiisoft](https://wordpress.org/support/users/mkiisoft/)
 * Last activity: [10 years, 11 months ago](https://wordpress.org/support/topic/orderbymeta_value_num-and-users-posts-errors/#post-6288984)
 * Status: not resolved