Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Talk:Template Tags/get posts

I am wondering if any has looked at the example for this tag. I am trying to figure out if there is a typo. It reads "if you want to list last 5 posts, but the code is getting the last 10. I am new to the "wiki"/"codex" environment, so I don't want to do anything or try to do anything until I get some feedback. I tried to bring it up at the support forum, but received no feedback.Miklb 05:22, 8 Jun 2005 (UTC)

  • The description called for displaying 5 posts, however, the tag was pulling 10. Fixed the article. --Schulte 15:00, 14 Jun 2005 (UTC)

Note that if you use the $posts variable, and you include this code before your main article code, you will break things. Better to use another variable if you do not want to break the normal article loop after this code is done. -- Nils 04:56, 13 Dec 2005 (GMT)

posts_per_page should be numberposts

Correct me if I'm wrong, but shouldn't it say numberposts where it instead says posts_per_page? I've looked into the source code and that appears to be what it uses to limit the number of posts. Marcus1060 04:17, 15 December 2010 (UTC)

This seemed to work for me too (and thanks for posting this on the forum as I was tearing my hair out trying to fix that!) so I've made that edit. Hopsyturvy 11:31, 10 January 2011 (UTC)
Both work. get_posts() accepts 'numberposts', then copies it to 'posts_per_page' because WP_Query uses 'posts_per_page'. iandunn 12:08, 30 January 2013 (UTC)
Could there be a note somewhere as well that using -1 with numberposts/posts_per_page will query all posts? That information is omitted from this page and is very useful to know. Nessthehero 23:51, 16 July 2013 (UTC)

Getting future posts

Can anyone elaborate on how to force future posts to be returned by get_posts as well? I want to put a list of coming events in my sidebar (coming events are all in one category). Thanks. — Sam Wilson 06:09, 20 Aug 2007 (UTC)

orderby=post_title

Suggested Additon

I think that, as of 2.6, orderby=post_title must instead be orderby=title.

Explained

It seems that 2.6 has drastically changed the get_posts() function, now using an instance of WP_Query to query the database. The documentation here says that you can use "orderby=post_title", however this stopped working as of 2.6. I believe that it's because WP_Query doesn't support "post_title".

Line 1241 of query.php reads... $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand');

Where do I put this code?

Forgive me for my slight cluelessness, I'd love to set it up so that I can have just one post per category followed by a list of other posts, but I'm not sure where to put the code? Particularly since it appears to be calling a specific category, I'm not sure where it should go.

Is there a way to do this so that it will work for every category?

User Jenyum - your question is better addressed to the WordPress Support Forums. --MichaelH (talk) 13:44, 2 December 2008 (UTC)

custom taxonomies don't work correctly with get_posts()

@Scribu -

I saw that you undid my recent addition to this page. No hard feelings, it may not be the right place to do this - but the fact remains that get_posts() produces unexpected results when using it with custom post types.

For example, calling this:

get_posts('numberposts=1&post_type=movie&genre=action');

will not work, especially if 'genre' is a hierarchical taxonomy. I've had to revert to using raw mysql queries instead of get_posts, query_posts, or re-instantiating WP_Query.

Hence the text I've struck through on this page (what you reverted).

Again: for sure, the right place to address such issues is Trac, but shouldn't the function reference page reflect the outcome one can expect from using the function, and warn users about possible problems?

Needs Significant Revision / Attention

This page, which is a critical page for any theme developer, needs serious attention by those who know the API well (I unfortunately do not, which is why I'm quite confounded). The last section talks about < 2.5 parameters, listing them cryptically as PHP variables eg: $category. But the function signature at the top of the page declares: get_posts($arguments), and later you discover that $arguments can be a hash of parameters (that are listed on the_query page - more cross-referencing). None of this corresponds with the < 2.5 parameters, and yet the documentation states that you can use all of the pre 2.5 parameters (eg: $category) along with this new-style declaration. Please help all future developers by cleaning this confusion up! I would do my part here, but I'm one of those who are confused! Tomaugerdotcom@yahoo.ca 21:45, 7 December 2010 (UTC)

The pre 2.5 stuff should probably be deleted. You can check the xref for details, the function is quite messy (xref). Hope that helps Nkuttler 22:53, 7 December 2010 (UTC)

Isn't "author" an argument of get_posts?

Isn't "author" an argument of get_posts()?

Returning private posts

I believe the claim in the "Return Value" section that "get_posts() will return private pages in the appropriate context (i.e., for an administrator)" is no longer true. See the get_posts() function in wp-includes/post.php Topdownjimmy 16:43, 6 November 2013 (UTC)

Include makes order and orderby inoperative

Passing an array of ids as include value makes any values for the order and orderby arguments mute. Wouldn't the expected behaviour be to sort the included ids like any "WHERE ... IN (.., .., ..) ORDER BY ... " type of MySQL-query?