{"id":61,"date":"2013-12-16T03:11:59","date_gmt":"2013-12-16T03:11:59","guid":{"rendered":"http:\/\/www.eminozlem.com\/?p=61"},"modified":"2015-06-06T21:30:22","modified_gmt":"2015-06-06T21:30:22","slug":"how-to-get-random-tags-in-wordpress","status":"publish","type":"post","link":"https:\/\/eminozlem.com\/tr\/how-to-get-random-tags-in-wordpress\/","title":{"rendered":"How to get random tags in wordpress"},"content":{"rendered":"<p>I needed a placeholder for tags input but it&#8217;s boring to display the same tags every time. But unlike posts, WP doesnt have &#8220;rand&#8221; orderby paramater. So what you can do; is to get a #no of tags and shuffle them.<\/p>\n<p><a href=\"http:\/\/static.eminozlem.com\/uploads\/2013\/12\/tagdUntitled-6.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-63 cboxElement\" alt=\"tagdUntitled-6\" src=\"http:\/\/static.eminozlem.com\/uploads\/2013\/12\/tagdUntitled-6-300x67.jpg\" width=\"300\" height=\"67\" \/><\/a><\/p>\n<p>So basically what we do is:<\/p>\n<ol>\n<li>Check if we have a recent transient<\/li>\n<li>If not, query the tags.<\/li>\n<li>Note the allowed orderby parameters for tags are defined as $tag_rnd_arr, which will add more randomness to them<\/li>\n<li>11 is the number of tags to get, 3 is the number of tags to return. You can increase or decrease them<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true\">function eobtn_get_rand_tags() {\r\n\t$rand_tags = get_transient(\"rand_tags\");\r\n\t\/\/ Check if we have queried 3 random tags recently\r\n\tif(!$rand_tags) {\r\n\t\t$tag_rnd_arr = array(\"name\",\"count\",\"term_id\");\r\n\t\t$tag_argz=array(\r\n\t\t\"orderby\" =&gt; array_rand($tag_rnd_arr),\r\n\t\t\"order\" =&gt; \"DESC\",\r\n\t\t\"number\" =&gt; 11,\r\n\t\t\"hierarchical\" =&gt; false,\r\n\t\t\"fields\" =&gt; \"names\"\r\n\t\t);\r\n\t\t$tags = get_tags($tag_argz);\r\n\t\t$rand_tags = array();\r\n\t\tshuffle($tags);\r\n\t\t$ran_key_arr = array_rand($tags, 3);\r\n\t\tforeach ($ran_key_arr as $ran_tag ) {\r\n\t\t\t$rand_tags[] = $tags[$ran_tag];\r\n\t\t}\r\n\t\t\/\/ Save the result for a short time (3 secs) to save an extra query hit if you have thousands of tags and \/ or visitors\r\n\t\tset_transient(\"rand_tags\",$rand_tags, 3);\r\n\t}\r\n\treturn $rand_tags;\r\n}<\/pre>\n<p>If you need echoing them instead of returning, you can simply echo them using<\/p>\n<pre class=\"lang:default decode:true \">\tif ($rand_tags) {\r\n\t  foreach($rand_tags as $tag) {\r\n\t\t  echo '&lt;a href=\"'.get_tag_link($tag-&gt;term_id).'\"&gt;'.$tag-&gt;name . '&lt;\/a&gt;';\r\n\t\t  if($tc != 3 || $ttc != 1)  echo ', ';\r\n\t  }\r\n\t}<\/pre>\n<p>It should look like this<a href=\"http:\/\/static.eminozlem.com\/uploads\/2013\/12\/taggClipboard01.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-70 cboxElement cbinl\" alt=\"taggClipboard01\" src=\"http:\/\/static.eminozlem.com\/uploads\/2013\/12\/taggClipboard01-300x101.jpg\" width=\"300\" height=\"101\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I needed a placeholder for tags input but it&#8217;s boring to display the same tags every time. But unlike posts, WP doesnt have &#8220;rand&#8221; orderby paramater. So what you can do; is to get a #no of tags and shuffle them. So basically what we do is: Check if we have a recent transient If&#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":[7,1,2],"tags":[16,14,15],"class_list":["post-61","post","type-post","status-publish","format-standard","hentry","category-coding","category-web","category-wordpress","tag-random","tag-tags","tag-wp-query"],"acf":[],"_links":{"self":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts\/61","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=61"}],"version-history":[{"count":0,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"wp:attachment":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}