{"id":293,"date":"2015-09-22T12:43:18","date_gmt":"2015-09-22T12:43:18","guid":{"rendered":"http:\/\/www.eminozlem.com\/?p=293"},"modified":"2015-09-22T12:57:44","modified_gmt":"2015-09-22T12:57:44","slug":"fastest-way-to-get-1-post-id-in-wordpress","status":"publish","type":"post","link":"https:\/\/eminozlem.com\/tr\/fastest-way-to-get-1-post-id-in-wordpress\/","title":{"rendered":"Fastest way to get 1 post->ID in WordPress"},"content":{"rendered":"<p>So all you need is a post-&gt;ID whats the fastest way to get it ?<\/p>\n<p><a class=\"btn btn-info btn-sm\" href=\"#conclusion\"><i class=\"fa fa-chevron-down\"><\/i> Jump to conclusion<\/a> or lets evaluate our options here:<\/p>\n<p><code>query_posts()<\/code>, <code>get_posts()<\/code>,<code>wp_query()<\/code>, <code>$wpdb<\/code><\/p>\n<p>As a rule of thumb, usually we can say $wpdb &gt; wp_query &gt; get_posts &gt; query_posts, meaning; you should almost never ever use <code>query_posts<\/code>. And usually between the two, <code>wp_query.<\/code> performs better over <code>get_posts<\/code> in most situations, and querying the db directly via $wpdb is often the most efficient way eventhough wp_query is safer.<\/p>\n<p>For results there is no difference, you will end up with 1 ID.<\/p>\n<p><a href=\"http:\/\/static.eminozlem.com\/uploads\/2015\/09\/gp1idsspUntitled-1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"642\" height=\"310\" class=\"alignnone size-full wp-image-298\" src=\"http:\/\/static.eminozlem.com\/uploads\/2015\/09\/gp1idsspUntitled-1.jpg\" alt=\"gp1idsspUntitled-1\" \/><\/a><\/p>\n<p>Like I said above query_posts is almost never needed, lets get that out of the way with a simple query_posts vs get_posts test:<\/p>\n<p><a href=\"http:\/\/static.eminozlem.com\/uploads\/2015\/09\/gpvssgpp.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"896\" height=\"146\" class=\"alignnone size-full wp-image-297\" src=\"http:\/\/static.eminozlem.com\/uploads\/2015\/09\/gpvssgpp.jpg\" alt=\"gpvssgpp\" srcset=\"https:\/\/static.eminozlem.com\/uploads\/2015\/09\/gpvssgpp.jpg 896w, https:\/\/static.eminozlem.com\/uploads\/2015\/09\/gpvssgpp-720x117.jpg 720w\" sizes=\"auto, (max-width: 896px) 100vw, 896px\" \/><\/a><\/p>\n<p>That leaves us with wp_query, get_posts and $wpdb. Lets take a look at get_posts vs wp_query. (Please note that I&#8217;ve used 3 different WP_query args sets, you can see all the functions below)<\/p>\n<p><a href=\"http:\/\/static.eminozlem.com\/uploads\/2015\/09\/gg2Clipboard01.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"919\" height=\"150\" class=\"alignnone size-full wp-image-300\" src=\"http:\/\/static.eminozlem.com\/uploads\/2015\/09\/gg2Clipboard01.jpg\" alt=\"gg2Clipboard01\" srcset=\"https:\/\/static.eminozlem.com\/uploads\/2015\/09\/gg2Clipboard01.jpg 919w, https:\/\/static.eminozlem.com\/uploads\/2015\/09\/gg2Clipboard01-720x118.jpg 720w\" sizes=\"auto, (max-width: 919px) 100vw, 919px\" \/><\/a><\/p>\n<p>The difference is not all that big, but get_posts is the winner compared to __wp_query_add_args_no_cache .( wpquery with additional args for optimal performance )<\/p>\n<p>So lets pick a winner is it gonna be get_posts or $wpdb ? As expected $wpdb is the clear winner here:<\/p>\n<p><a href=\"http:\/\/static.eminozlem.com\/uploads\/2015\/09\/wpdbwwUntitled-1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"958\" height=\"320\" class=\"alignnone size-full wp-image-302\" src=\"http:\/\/static.eminozlem.com\/uploads\/2015\/09\/wpdbwwUntitled-1.jpg\" alt=\"wpdbwwUntitled-1\" srcset=\"https:\/\/static.eminozlem.com\/uploads\/2015\/09\/wpdbwwUntitled-1.jpg 958w, https:\/\/static.eminozlem.com\/uploads\/2015\/09\/wpdbwwUntitled-1-720x241.jpg 720w\" sizes=\"auto, (max-width: 958px) 100vw, 958px\" \/><\/a><\/p>\n<p id=\"conclusion\" class=\"conc lead\">Concluison: If all you need is 1 random post-&gt;ID, simply use $wpdb<\/p>\n<p>Here are all the functions used:<\/p>\n<pre class=\"lang:php decode:true \">\tfunction eo_get_an_id_queryposts() {\r\n\t\t$get_one_id_args = array(\r\n\t\t\t'orderby' =&gt; 'rand',\r\n\t\t\t'posts_per_page' =&gt; '1',\r\n\t\t\t'fields' =&gt; 'ids'\r\n\t\t);\r\n\t\t$rand_post_id_arr = query_posts( $get_one_id_args );\r\n\t\t$rand_post_id = $rand_post_id_arr[0];\r\n\t\t\/\/ intval it because id returns as string\r\n\t\treturn intval($rand_post_id);\r\n\t}\r\n\tfunction eo_get_an_id_getposts() {\r\n\t\t$get_one_id_args = array(\r\n\t\t\t'orderby' =&gt; 'rand',\r\n\t\t\t'posts_per_page' =&gt; '1',\r\n\t\t\t'fields' =&gt; 'ids'\r\n\t\t);\r\n\t\t$rand_post_id_arr = get_posts( $get_one_id_args );\r\n\t\t$rand_post_id = $rand_post_id_arr[0];\r\n\t\t\/\/ intval it because id returns as string\r\n\t\treturn intval($rand_post_id);\r\n\t}\r\n\t\r\n\tfunction eo_get_an_id_wpquery_min_args() {\r\n\t\t$get_one_id_args = array(\r\n\t\t\t'orderby' =&gt; 'rand',\r\n\t\t\t'posts_per_page' =&gt; '1',\r\n\t\t);\r\n\t\t$get_an_id_qy = new WP_Query( $get_one_id_args );\r\n\t\t$get_an_id_qy_result_arr = $get_an_id_qy-&gt;posts;\r\n\t\t\/\/ no point on going forward if no posts found.\r\n\t\tif( empty($get_an_id_qy_result_arr) ) return false;\r\n\t\t\/\/ Get the-&gt;ID from the post object.\r\n\t\t$rand_post_obj = $get_an_id_qy_result_arr[0];\r\n\t\t$rand_post_id = $rand_post_obj-&gt;ID ;\r\n\t\treturn $rand_post_id;\r\n\t}\r\n\tfunction eo_get_an_id_wpquery_add_args() {\r\n\t\t$get_one_id_args = array(\r\n\t\t\t'orderby' =&gt; 'rand',\r\n\t\t\t'posts_per_page' =&gt; '1',\r\n\t\t\t'fields' =&gt; 'ids'\r\n\t\t);\r\n\t\t$get_an_id_qy = new WP_Query( $get_one_id_args );\r\n\t\t$get_an_id_qy_result_arr = $get_an_id_qy-&gt;posts;\r\n\t\t\/\/ no point on going forward if no posts found.\r\n\t\tif( empty($get_an_id_qy_result_arr) ) return false;\r\n\t\t$rand_post_id = $get_an_id_qy_result_arr[0];\r\n\t\t\/\/ intval it because id returns as string\r\n\t\treturn intval($rand_post_id);\r\n\r\n\t}\r\n\t\r\n\tfunction eo_get_an_id_wpquery_add_args_nocache() {\r\n\t\t$get_one_id_args = array(\r\n\t\t\t'orderby' =&gt; 'rand',\r\n\t\t\t'posts_per_page' =&gt; '1',\r\n\t\t\t'fields' =&gt; 'ids',\r\n\t\t\t'no_found_rows' =&gt; true,\r\n\t\t\t'cache_results'=&gt;false,\r\n\t\t\t'update_post_term_cache' =&gt; false,\r\n\t\t\t'update_post_meta_cache' =&gt; false,\r\n\t\t);\r\n\t\t$get_an_id_qy = new WP_Query( $get_one_id_args );\r\n\t\t$get_an_id_qy_result_arr = $get_an_id_qy-&gt;posts;\r\n\t\t\/\/ no point on going forward if no posts found.\r\n\t\tif( empty($get_an_id_qy_result_arr) ) return false;\r\n\t\t$rand_post_id = $get_an_id_qy_result_arr[0];\r\n\t\t\/\/ intval it because id returns as string\r\n\t\treturn intval($rand_post_id);\r\n\r\n\t}\r\n\tfunction eo_get_an_id_add_wpdb() {\r\n\t\tglobal $wpdb;\r\n\t\t$rand_post_id = $wpdb-&gt;get_var(\"SELECT ID FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY RAND() LIMIT 1\");\r\n\t\t\/\/ intval it because id returns as string\r\n\t\treturn intval($rand_post_id);\r\n\t}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So all you need is a post-&gt;ID whats the fastest way to get it ? Jump to conclusion or lets evaluate our options here: query_posts(), get_posts(),wp_query(), $wpdb As a rule of thumb, usually we can say $wpdb &gt; wp_query &gt; get_posts &gt; query_posts, meaning; you should almost never ever use query_posts. And usually between the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":301,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts\/293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/comments?post=293"}],"version-history":[{"count":0,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts\/293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/media\/301"}],"wp:attachment":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/media?parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/categories?post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/tags?post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}