{"id":681,"date":"2022-05-30T09:00:00","date_gmt":"2022-05-30T09:00:00","guid":{"rendered":"https:\/\/www.naveedulhaq.com\/?p=681"},"modified":"2022-09-21T08:57:46","modified_gmt":"2022-09-21T08:57:46","slug":"wp-rest-api-order-users-by-meta-value-acf","status":"publish","type":"post","link":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/","title":{"rendered":"WP REST API: Order Users by meta value (acf)"},"content":{"rendered":"\n<p>I have an issue where I need to return users based on a custom field. I have created these custom fields with an excellent plugin called &#8220;Advanced Custom Field&#8221; (ACF).<\/p>\n\n\n\n<p>ACF plugin allows you to show custom fields in WordPress REST API as well but you do not have any out of the box way to sort users by these custom fields.<\/p>\n\n\n\n<p>when I dig into details on how ACF generate these fields, these are usermeta created against a user and stored in usermeta database.<\/p>\n\n\n\n<p>Based on the above fact I created a custom REST API endpoint that returns users by a specified sort order and based on a custom usermeta (or custom field)<\/p>\n\n\n\n<p>In this custom API endpoint I just write a MySQL query to get results I need such as <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT *, \nCONVERT(SUBSTRING_INDEX(meta_value,'-',-1),UNSIGNED INTEGER) as userbalance \nFROM `usermeta`\nWHERE\nmeta_key = 'userbalance'\nORDER BY userbalance DESC\nLIMIT 10 OFFSET 0<\/code><\/pre>\n\n\n\n<p>Now I tested the above query in PhpMyadmin and make sure it works fine.<\/p>\n\n\n\n<p>The next step is to create a custom WordPress REST API endpoint.<\/p>\n\n\n\n<p>Below is the code I have written on the function.php page in the WordPress theme folder<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getgameusers_func($request) {\n\t$sort = $request-&gt;get_param('sort');\n    $page = $request-&gt;get_param('page');\n\tglobal $wpdb;\n\t\n\tif($page&gt;0)\n\t{\n\t\t$page = ($page-1)*10;\n\t}\n\n    $query = $wpdb-&gt;get_results( \"SELECT *, CONVERT(SUBSTRING_INDEX(meta_value,'-',-1),UNSIGNED INTEGER) as userbalance FROM `usermeta` WHERE meta_key = 'userbalance' ORDER BY userbalance DESC LIMIT 10 OFFSET {$page}\" );\n\t\n\treturn $query;\n\t\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action('rest_api_init', function () {\n     register_rest_route( '\/wp\/v2', '\/userbalance\/', array(\n       'methods' =&gt; 'GET',\n       'callback' =&gt; 'getgameusers_func'\n     ));\n});\n<\/code><\/pre>\n\n\n\n<p>The above code will create a custom endpoint \/wp\/v2\/userbalance that returns users with a custom sort order.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have an issue where I need to return users based on a custom field. I have created these custom fields with an excellent plugin&#8230;<\/p>\n","protected":false},"author":1,"featured_media":633,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[32,23,70,80],"class_list":["post-681","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-developer","tag-development","tag-php","tag-wordpress"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>WP REST API: Order Users by meta value (acf) - Naveed Ul-Haq&#039;s blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WP REST API: Order Users by meta value (acf) - Naveed Ul-Haq&#039;s blog\" \/>\n<meta property=\"og:description\" content=\"I have an issue where I need to return users based on a custom field. I have created these custom fields with an excellent plugin...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/\" \/>\n<meta property=\"og:site_name\" content=\"Naveed Ul-Haq&#039;s blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-05-30T09:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-21T08:57:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2021\/11\/php.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1750\" \/>\n\t<meta property=\"og:image:height\" content=\"875\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Naveed Ul-Haq\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Ul-Haq\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/\"},\"author\":{\"name\":\"Naveed Ul-Haq\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#\\\/schema\\\/person\\\/dd6db5980b965fcae41e096d357c65c9\"},\"headline\":\"WP REST API: Order Users by meta value (acf)\",\"datePublished\":\"2022-05-30T09:00:00+00:00\",\"dateModified\":\"2022-09-21T08:57:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/\"},\"wordCount\":203,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#\\\/schema\\\/person\\\/dd6db5980b965fcae41e096d357c65c9\"},\"image\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.naveedulhaq.com\\\/wp-content\\\/uploads\\\/2021\\\/11\\\/php.png\",\"keywords\":[\"developer\",\"development\",\"php\",\"wordpress\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/\",\"url\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/\",\"name\":\"WP REST API: Order Users by meta value (acf) - Naveed Ul-Haq&#039;s blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.naveedulhaq.com\\\/wp-content\\\/uploads\\\/2021\\\/11\\\/php.png\",\"datePublished\":\"2022-05-30T09:00:00+00:00\",\"dateModified\":\"2022-09-21T08:57:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.naveedulhaq.com\\\/wp-content\\\/uploads\\\/2021\\\/11\\\/php.png\",\"contentUrl\":\"https:\\\/\\\/www.naveedulhaq.com\\\/wp-content\\\/uploads\\\/2021\\\/11\\\/php.png\",\"width\":1750,\"height\":875,\"caption\":\"php\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/index.php\\\/php\\\/wp-rest-api-order-users-by-meta-value-acf\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.naveedulhaq.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WP REST API: Order Users by meta value (acf)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#website\",\"url\":\"https:\\\/\\\/www.naveedulhaq.com\\\/\",\"name\":\"Naveed Ul-Haq's blog\",\"description\":\"AI, Optimizely, Azure &amp; more\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#\\\/schema\\\/person\\\/dd6db5980b965fcae41e096d357c65c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.naveedulhaq.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.naveedulhaq.com\\\/#\\\/schema\\\/person\\\/dd6db5980b965fcae41e096d357c65c9\",\"name\":\"Naveed Ul-Haq\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g\",\"caption\":\"Naveed Ul-Haq\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g\"},\"description\":\"I lead engineering delivery teams for digital commerce and digital experience platforms, combining hands-on architecture experience with strong delivery governance. Over 20+ years, I\u2019ve built and led cross\u2011functional teams (engineering, BA, QA) delivering modern cloud solutions on Azure, microservices, APIs and eCommerce\\\/CMS platforms. My recent focus includes AI-enabled commerce automation: product ingestion and enrichment workflows, content optimisation for SEO and shopping feeds, and agentic tooling to improve marketing and accessibility workflows. I\u2019m passionate about building high-performing teams, establishing quality\\\/release standards, and delivering measurable outcomes across performance, reliability, and speed of change.\",\"sameAs\":[\"https:\\\/\\\/www.naveedulhaq.com\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedulhaq\\\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WP REST API: Order Users by meta value (acf) - Naveed Ul-Haq&#039;s blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/","og_locale":"en_GB","og_type":"article","og_title":"WP REST API: Order Users by meta value (acf) - Naveed Ul-Haq&#039;s blog","og_description":"I have an issue where I need to return users based on a custom field. I have created these custom fields with an excellent plugin...","og_url":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/","og_site_name":"Naveed Ul-Haq&#039;s blog","article_published_time":"2022-05-30T09:00:00+00:00","article_modified_time":"2022-09-21T08:57:46+00:00","og_image":[{"width":1750,"height":875,"url":"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2021\/11\/php.png","type":"image\/png"}],"author":"Naveed Ul-Haq","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Ul-Haq","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/#article","isPartOf":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/"},"author":{"name":"Naveed Ul-Haq","@id":"https:\/\/www.naveedulhaq.com\/#\/schema\/person\/dd6db5980b965fcae41e096d357c65c9"},"headline":"WP REST API: Order Users by meta value (acf)","datePublished":"2022-05-30T09:00:00+00:00","dateModified":"2022-09-21T08:57:46+00:00","mainEntityOfPage":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/"},"wordCount":203,"commentCount":0,"publisher":{"@id":"https:\/\/www.naveedulhaq.com\/#\/schema\/person\/dd6db5980b965fcae41e096d357c65c9"},"image":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/#primaryimage"},"thumbnailUrl":"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2021\/11\/php.png","keywords":["developer","development","php","wordpress"],"articleSection":["PHP"],"inLanguage":"en-GB","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/","url":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/","name":"WP REST API: Order Users by meta value (acf) - Naveed Ul-Haq&#039;s blog","isPartOf":{"@id":"https:\/\/www.naveedulhaq.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/#primaryimage"},"image":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/#primaryimage"},"thumbnailUrl":"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2021\/11\/php.png","datePublished":"2022-05-30T09:00:00+00:00","dateModified":"2022-09-21T08:57:46+00:00","breadcrumb":{"@id":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/#primaryimage","url":"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2021\/11\/php.png","contentUrl":"https:\/\/www.naveedulhaq.com\/wp-content\/uploads\/2021\/11\/php.png","width":1750,"height":875,"caption":"php"},{"@type":"BreadcrumbList","@id":"https:\/\/www.naveedulhaq.com\/index.php\/php\/wp-rest-api-order-users-by-meta-value-acf\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.naveedulhaq.com\/"},{"@type":"ListItem","position":2,"name":"WP REST API: Order Users by meta value (acf)"}]},{"@type":"WebSite","@id":"https:\/\/www.naveedulhaq.com\/#website","url":"https:\/\/www.naveedulhaq.com\/","name":"Naveed Ul-Haq's blog","description":"AI, Optimizely, Azure &amp; more","publisher":{"@id":"https:\/\/www.naveedulhaq.com\/#\/schema\/person\/dd6db5980b965fcae41e096d357c65c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.naveedulhaq.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":["Person","Organization"],"@id":"https:\/\/www.naveedulhaq.com\/#\/schema\/person\/dd6db5980b965fcae41e096d357c65c9","name":"Naveed Ul-Haq","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g","caption":"Naveed Ul-Haq"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/362536aba6cc66917d7558cacd015a81c7cdf1a69b9a28c994764847c487b692?s=96&d=mm&r=g"},"description":"I lead engineering delivery teams for digital commerce and digital experience platforms, combining hands-on architecture experience with strong delivery governance. Over 20+ years, I\u2019ve built and led cross\u2011functional teams (engineering, BA, QA) delivering modern cloud solutions on Azure, microservices, APIs and eCommerce\/CMS platforms. My recent focus includes AI-enabled commerce automation: product ingestion and enrichment workflows, content optimisation for SEO and shopping feeds, and agentic tooling to improve marketing and accessibility workflows. I\u2019m passionate about building high-performing teams, establishing quality\/release standards, and delivering measurable outcomes across performance, reliability, and speed of change.","sameAs":["https:\/\/www.naveedulhaq.com","https:\/\/www.linkedin.com\/in\/naveedulhaq\/"]}]}},"_links":{"self":[{"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/posts\/681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/comments?post=681"}],"version-history":[{"count":0,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/posts\/681\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/media\/633"}],"wp:attachment":[{"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/media?parent=681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/categories?post=681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.naveedulhaq.com\/index.php\/wp-json\/wp\/v2\/tags?post=681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}