{"id":4201,"date":"2020-02-27T12:08:16","date_gmt":"2020-02-27T17:08:16","guid":{"rendered":"https:\/\/themespress.ca\/?p=4201"},"modified":"2021-11-17T06:57:13","modified_gmt":"2021-11-17T11:57:13","slug":"sort-wordpress-users-by-id","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/sort-wordpress-users-by-id\/","title":{"rendered":"Sort WordPress users by ID"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>This tutorial will show you how to sort WordPress users by ID and DESC. Thanks to the <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/pre_get_posts\/\" target=\"_blank\" rel=\"noopener noreferrer\">pre_get_posts<\/a> hook, it will allow us to custom a query for and posts types. The <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/pre_get_posts\/\" target=\"_blank\" rel=\"noopener noreferrer\">pre_get_posts<\/a> can generate WordPress admin results; i.e. on the website&#8217;s front end.In this case, we will be using <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/pre_get_users\/\" target=\"_blank\" rel=\"noopener noreferrer\">pre_get_users<\/a> which is designed to return users.<\/p>\n<p>In the example below, we&#8217;ll be looking to sort our WordPress users into a table. First off, we need a condition that indicates that if the user isn&#8217;t an Admin, do nothing.<\/p>\n<p>Then we use two arguments: the orderby ID and the order DESC.<br \/>\nVoil\u00e0! Your users are now sorted at the top of the table with the newly created users first.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-4482579\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-4482579\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ Admin users order by ID and DESC Pre Get Users \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n \nfunction sitebook_order_users_by_id( $query ) {\n \n   \/\/Check that we are in admin otherwise return\n   if( !is_admin() ) {\n      return;\n   }\n \n   \/\/ We are changing the query_vars to reorder\n   $query-&amp;gt;query_vars[&#039;orderby&#039;] = &#039;ID&#039;;\n   $query-&amp;gt;query_vars[&#039;order&#039;]   = &#039;DESC&#039;;\n \n   \/\/ We need to remember to return the altered query.\n   return $query;\n}\n\/\/ Lets apply our function to hook.\nadd_action( &#039;pre_get_users&#039;, &#039;sitebook_order_users_by_id&#039; );<\/pre><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"This tutorial will show you how to sort Wordpress users by ID and DESC. Thanks to the pre_get_posts hook, it will allow us to custom a query for and posts<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/sort-wordpress-users-by-id\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4933,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[326],"class_list":["post-4201","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-users"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/4201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=4201"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/4201\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4933"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=4201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=4201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=4201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}