{"id":22640,"date":"2021-02-08T14:00:01","date_gmt":"2021-02-08T19:00:01","guid":{"rendered":"https:\/\/pressable.com\/?post_type=knowledgebase&#038;p=22640"},"modified":"2025-09-11T15:12:58","modified_gmt":"2025-09-11T20:12:58","slug":"randomizing-posts","status":"publish","type":"knowledgebase","link":"https:\/\/pressable.com\/knowledgebase\/randomizing-posts\/","title":{"rendered":"Randomizing Posts in WordPress"},"content":{"rendered":"\n<p>Because query results end up cached in the object cache, it is difficult to use<a href=\"https:\/\/carriedils.com\/wordpress-orderby-rand-and-wp-engine\/\" target=\"_blank\" rel=\"noopener\"> &#8216;orderby&#8217;=&gt;&#8217;rand&#8217;<\/a> in a query. The first randomized result will then be cached and returned on each page reload until the cache is cleared. The solution to randomizing posts in WordPress is to add a filter to the theme\u2019s functions.php file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add_filter( 'the_posts', function( $posts, \\WP_Query $query )\n{\n    if( $random = $query-&gt;get( 'pressable_random_posts' ) )\n    {\n        shuffle( $posts );\n        $posts = array_slice( $posts, 0, (int) $random );\n    }\n    return $posts;\n}, 10, 2 );<\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>And then to the query add:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'pressable_random_posts' =&gt; 3<\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>With 3 being the number of items you wish to display.<\/p>\n\n\n\n<p>An example of a modified query:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$custom_args = array(\n'post_type'      =&gt; 'products',\n'post_status'    =&gt; 'publish',\n'posts_per_page' =&gt; 20,\n'orderby'        =&gt; 'rand',\n'no_found_rows'  =&gt; true,\n'pressable_random_posts' =&gt; 3\n);<\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>To apply a filter to a query without modifying the query directly, to your funtions.php file you can do something similar to:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function pressable_apply_filter_automatically($query) {\n\n   if ( is_front_page()  &amp;&amp;  in_array ( $query-&gt;get('post_type'), array('products') ) ) {\n       $query-&gt;set('pressable_random_posts', 3);\n   }\n}\nadd_action( 'pre_get_posts', 'pressable_apply_filter_automatically' );<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Because query results end up cached in the object cache, it is difficult to use &#8216;orderby&#8217;=&gt;&#8217;rand&#8217; in a query. The first randomized result will then be cached and returned on each page [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":67,"template":"","meta":{"content-type":"","footnotes":""},"knowledgebase-categories":[524],"kb-tags":[],"class_list":["post-22640","knowledgebase","type-knowledgebase","status-publish","hentry","knowledgebase-categories-tutorials"],"jetpack_sharing_enabled":true,"meta_box":{"kb_updater":"1"},"_links":{"self":[{"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/knowledgebase\/22640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/knowledgebase"}],"about":[{"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/types\/knowledgebase"}],"author":[{"embeddable":true,"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":2,"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/knowledgebase\/22640\/revisions"}],"predecessor-version":[{"id":42468,"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/knowledgebase\/22640\/revisions\/42468"}],"wp:attachment":[{"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/media?parent=22640"}],"wp:term":[{"taxonomy":"knowledgebase-categories","embeddable":true,"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/knowledgebase-categories?post=22640"},{"taxonomy":"kb-tags","embeddable":true,"href":"https:\/\/pressable.com\/wp-json\/wp\/v2\/kb-tags?post=22640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}