{"id":257,"date":"2015-08-23T10:29:09","date_gmt":"2015-08-23T10:29:09","guid":{"rendered":"http:\/\/www.eminozlem.com\/?p=257"},"modified":"2015-09-22T11:54:51","modified_gmt":"2015-09-22T11:54:51","slug":"fastest-way-to-get-1-random-post-in-wordpress","status":"publish","type":"post","link":"https:\/\/eminozlem.com\/tr\/fastest-way-to-get-1-random-post-in-wordpress\/","title":{"rendered":"Fastest way to get 1 random post in WordPress"},"content":{"rendered":"<p>So all you need is simply 1 random post, 1 random post-&gt;ID or 1 post link, you dont need to go through all the hoops. (Update: added $wpdb function to tests)<\/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 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. But, in our case, that&#8217;s not the case.<\/p>\n<p>First, lets establish the fact that the end result is the same for all 3 methods; you&#8217;ll get 1 random post &#8211; object. I used the very same query args for all 3 methods:<\/p>\n<pre class=\"lang:php decode:true \">\t\tfunction eo_get_a_post_queryposts(){\r\n\t\t\t$args = array( 'posts_per_page' =&gt; 1, 'orderby' =&gt; 'rand' );\r\n\t\t\t$rand_post = query_posts( $args );\r\n\t\t\treturn $rand_post;\r\n\t\t}\r\n\t\tfunction eo_get_a_post_getposts(){\r\n\t\t\t$args = array( 'posts_per_page' =&gt; 1, 'orderby' =&gt; 'rand' );\r\n\t\t\t$rand_post = get_posts( $args );\r\n\t\t\treturn $rand_post;\r\n\t\t}\r\n\t\tfunction eo_get_a_post_wpquery(){\r\n\t\t\t$args = array( 'posts_per_page' =&gt; 1, 'orderby' =&gt; 'rand' );\r\n\t\t\t$rand_post_query_result = new WP_Query( $args );\r\n\t\t\t$rand_post = $rand_post_query_result-&gt;posts[0];\r\n\t\t\treturn $rand_post;\r\n\t\t}\r\n\t\tfunction eo_get_a_post_wpdb() {\r\n\t\t\tglobal $wpdb;\r\n\t\t\t$rand_post = $wpdb-&gt;get_results(\"SELECT * FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY RAND() LIMIT 1\");\r\n\t\t\treturn $rand_post;\r\n\t\t}<\/pre>\n<p>And like I said the result you are gonna get is the same:<\/p>\n<p><a href=\"http:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-261 size-full\" src=\"http:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand1.jpg\" alt=\"eogetonepostrand1\" width=\"949\" height=\"617\" srcset=\"https:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand1.jpg 949w, https:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand1-692x450.jpg 692w, https:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand1-923x600.jpg 923w\" sizes=\"auto, (max-width: 949px) 100vw, 949px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>So, lets get down to the main point, the speed. Like you can imagine, <code><del datetime=\"2015-08-23T10:29:35+00:00\">query_posts<\/del><\/code> is off the table, you can see how poorly it does compared to <code>get_posts<\/code>:<\/p>\n<p><a href=\"http:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-262 size-full\" src=\"http:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand2.jpg\" alt=\"eogetonepostrand2\" width=\"953\" height=\"127\" srcset=\"https:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand2.jpg 953w, https:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand2-720x96.jpg 720w\" sizes=\"auto, (max-width: 953px) 100vw, 953px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The question was which was slightly better, get_posts or wp_query ? I thought wp_query would do better, like it usually does, since get_posts is essentially a wrapper for wp_query, but to my suprise, get_posts was faster:<\/p>\n<p><a href=\"http:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-263 size-full\" src=\"http:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand3.jpg\" alt=\"eogetonepostrand3\" width=\"953\" height=\"136\" srcset=\"https:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand3.jpg 953w, https:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand3-720x103.jpg 720w\" sizes=\"auto, (max-width: 953px) 100vw, 953px\" \/><\/a><\/p>\n<p>I ran it 10,000 times to make sure &#8211; instead of 100. But the result wouldn&#8217;t change if you ran it for a mil. time: <a href=\"http:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand4.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"955\" height=\"140\" class=\"alignnone size-full wp-image-265\" src=\"http:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand4.jpg\" alt=\"eogetonepostrand4\" srcset=\"https:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand4.jpg 955w, https:\/\/static.eminozlem.com\/uploads\/2015\/08\/eogetonepostrand4-720x106.jpg 720w\" sizes=\"auto, (max-width: 955px) 100vw, 955px\" \/><\/a><\/p>\n<p>Update: I&#8217;ve added a $wpdb function to directly query the db, result is the same, get_posts is still faster.<\/p>\n<p class=\"conc lead\"><a href=\"http:\/\/static.eminozlem.com\/uploads\/2015\/08\/ceClipboard01.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"908\" height=\"155\" class=\"alignnone size-full wp-image-290\" src=\"http:\/\/static.eminozlem.com\/uploads\/2015\/08\/ceClipboard01.jpg\" alt=\"ceClipboard01\" srcset=\"https:\/\/static.eminozlem.com\/uploads\/2015\/08\/ceClipboard01.jpg 908w, https:\/\/static.eminozlem.com\/uploads\/2015\/08\/ceClipboard01-720x123.jpg 720w\" sizes=\"auto, (max-width: 908px) 100vw, 908px\" \/><\/a><\/p>\n<p id=\"conclusion\" class=\"conc lead\">Concluison: If all you need is 1 random post (object), simply use get_posts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So all you need is simply 1 random post, 1 random post-&gt;ID or 1 post link, you dont need to go through all the hoops. (Update: added $wpdb function to tests) 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 wp_query &gt; get_posts&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"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-257","post","type-post","status-publish","format-standard","hentry","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts\/257","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=257"}],"version-history":[{"count":0,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"wp:attachment":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}