get_posts upgraded

The function get_posts is very useful, and in my opinion under-utilized. It also was incomplete, so I went ahead and completed it.

You can view and use my spanking new functions.php file if you want a complete get_posts.

get_posts is what should be used to get a bunch of posts (or even just one post) outside the wordpress loop (The Loop). It takes 5 arguments :

  1. numberposts – the number of posts you want returned
  2. offset – the number of posts you want to skip (like saying skip the first n posts from the results)
  3. category – the category from which you need these posts
  4. orderby – order the post by date, or name of any one of the posts table columns
  5. order – arrange the results in ascending (ASC) or descending (DESC) order

For example,

$posts =
foreach ($posts as $post) :
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

will retrieve posts in category 4 sorted by name in ascending order and give you posts 11 through 16, formatted as specified in the code.

If you are using a nightly of v1.3, here’s the diff.

You can sure put get_links to some innovative and interesting use. The FAQ page uses that to display the 5 most recent answers, for example. Other uses could be to pull up the most recent n posts in each category for display in your “menu”, and so on.

Have fun! Now I get back to C++ programming, to solve what seemed like a small problem a week ago. Small problem indeed! I am firing all my neurons and yet there is no perceivable improvement.

This entry was posted in Uncategorized and tagged . Bookmark the permalink.

32 Responses to get_posts upgraded

  1. Mario says:

    Hi, I´ve a problem using your get_posts. I want to show the title and the content of the last post of a single category. So I pasted this in index.php:

    < ?php $posts = get_posts('order=ASC&category=3&orderby=name&offset=0&numberposts=1'); foreach ($posts as $post) : ?>
    < ?php the_title(); ?>
    < ?php the_content(); ?> endforeach; ?>

    I added the_content() line expecting to view the last post content, but for my surprise, was the content of my first post, it wasn´t even the right category! Can you help me? Thanks for helping in the support site!

  2. Mario says:

    Ok, the text I pasted is not visible (was part in php). To help you figure out, I added the line “the_content();” below “the_title();” and you can view the result in my right bar here:

  3. Carthik says:

    My best guess is that you still have the old functions.php file in your wp-includes folder, that does not have all the options that this get_posts I describe has.
    So please delete the old functions.php and get the new one.

  4. Alicson says:

    Carthik, this looks really good…
    could a “random” option be placed in the “sort” function, or something, to call up random posts.. i know there are “random posts” plugins out there, but they actually pull up only titles or excerpts.. not the entire article format.. it looks like get_posts would be able to do it.. and it looks like you know how to teach get_posts new tricks. think this is something you’d have time to do? :) *hoping hoping*
    regardless, thank you for having improved the get_post function in the first place.

  5. Hi, I’ve been trying to get thie to work with my site, but it keeps generating errors with get_post:

    Warning: Invalid argument supplied for foreach() in /sitepath/index.php on line 207

    i’m not sure what’s going on. i’m basically caopying exactly what you have on this page to test it in mine, but it generates this error instead.

  6. Vanhalle Jean-Christophe says:

    Hi :)
    How can i retrieve the id of individual post that the loop retrieve?
    I am using this code:
    < ?php $posts = get_posts('order=DSC&category=4&orderby=date&offset=0&numberposts=5');foreach ($posts as $post) :?>