{"id":313,"date":"2014-09-20T15:08:29","date_gmt":"2014-09-20T22:08:29","guid":{"rendered":"http:\/\/shanelabs.com\/blog\/?p=313"},"modified":"2014-09-20T15:08:29","modified_gmt":"2014-09-20T22:08:29","slug":"wordpress-development-getting-all-posts-from-get_posts","status":"publish","type":"post","link":"https:\/\/shanelabs.com\/blog\/2014\/09\/20\/wordpress-development-getting-all-posts-from-get_posts\/","title":{"rendered":"WordPress Development: Getting all posts from get_posts()"},"content":{"rendered":"<p>I&#8217;m writing code for a wordpress plugin of mine, and needed for the get_posts() function to return ALL posts. Here&#8217;s what I was using before:<\/p>\n<blockquote><p>$args = array(<br \/>\n&#8216;post_type&#8217; =&gt; &#8216;post&#8217;,<br \/>\n&#8216;post_status&#8217; =&gt; &#8216;publish&#8217;,<br \/>\n);<br \/>\n$posts = get_posts($args);<\/p><\/blockquote>\n<p>According to the wordpress documentation, the &#8216;posts_per_page&#8217; option for this function has a default value of 5, meaning you&#8217;ll get at most 5 results back. I looked to see if there was a way to show all posts, and I couldn&#8217;t find it documented anywhere.<\/p>\n<p>However, I did see some people provide code that had that value set at -1.\u00a0I thought it might be a way of specifying no limit, so I tried it, and it&#8217;s working. Here&#8217;s what I use now:<\/p>\n<blockquote><p>$args = array(<br \/>\n&#8216;post_type&#8217; =&gt; &#8216;post&#8217;,<br \/>\n&#8216;post_status&#8217; =&gt; &#8216;publish&#8217;,<br \/>\n<strong>&#8216;posts_per_page&#8217; =&gt; -1<\/strong><br \/>\n);<br \/>\n$posts = get_posts($args);<\/p><\/blockquote>\n<p>It may not be a documented features, but it&#8217;s working for now. Give it a shot if you&#8217;re looking to have get_posts() return an unlimited number of posts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m writing code for a wordpress plugin of mine, and needed for the get_posts() function to return ALL posts. Here&#8217;s what I was using before: $args = array( &#8216;post_type&#8217; =&gt; &#8216;post&#8217;, &#8216;post_status&#8217; =&gt; &#8216;publish&#8217;, ); $posts = get_posts($args); According to the wordpress documentation, the &#8216;posts_per_page&#8217; option for this function has a default value of 5, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-313","post","type-post","status-publish","format-standard","hentry","category-web"],"_links":{"self":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/comments?post=313"}],"version-history":[{"count":2,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/313\/revisions"}],"predecessor-version":[{"id":315,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/posts\/313\/revisions\/315"}],"wp:attachment":[{"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/media?parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/categories?post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanelabs.com\/blog\/wp-json\/wp\/v2\/tags?post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}