WordPress is a great tool with ton of community support. But sometimes, when you just can’t make it do your bidding, nobody has written a plugin to do what you want, and Google reveals no acceptable hacks, you just have to fire up Notepad and summon your inner PHP hacker. Coaxing WordPress into displaying certain posts on a static page is just one of those times.
WordPress static pages are not supposed to show dynamic stuff. But, sometimes, you want them to anyway. Here’s a plugin that does just that. This plugin adds the ability to put a tag in your page entry and have it display a list of posts using the get_posts template tag.
By default it will show the 10 most recent posts ordered in reverse date order, but it will accept any of the options provided by the get_posts template tag. To use it, just put the following into the HTML of any page or post, use as many times as you like
on the same page:
[get_posts]
This default usage will return the last 10 posts in reverse chronological order. Along with all the options provided by the get_posts template tag, it will also accept one additional option:
ul_class: This is the class assigned to the bullet list. Default of “get_posts_class”.
An example with a few options:
[get_posts tag="my favorite tag" numberposts=5 orderby="title]
Shows 5 posts with the tag “my favorite tag” ordered by title.
There are quite a few more options … check the wordpress.org extensions page for more detailed documentation on all the options.
Download the plugin here, and please let us know with a comment if you have any suggestions, comments, requests or questions. And, if you like it, hit the plugin page and give it a nice rating













Hi,
Just came across this template tonight while searching for a way to do this very thing. I am very new to wordpress however, so can you tell me exactly what I need to put on the page if I want it to pull all posts in the category ‘inspiration’
Thanks so much
Jeanette
Jeanette, all you have to do is add something like this to your page:
[get_posts category_name='inspiration']Take a look at the get_posts template tag page for more options … you can put any of the things listed in the parameters section into the tag.
Hi there
Absolutely love this plugin – thank you so much for creating it! Was wondering whether it’s possible to have more info showing than just the title of the post? Ideally, I am hoping to have a list of posts, with excerpts from each – and I’m not quite sure how to go about it. Is this an option?
Thanks!
Pia
Sorry, I meant to clarify that this is for when you are inserting the posts into a page – not for the sidebar.
Hi Pia, adding fields other than the title isn’t an option yet, but it’s a good idea and something we discussed when creating the plugin. My initial thought is to simply add another parameter in the tag that allows you to specify a comma-separated list of fields you’d like to show (with the currently shown title as the default), which would be a relatively easy addition. However, this doesn’t give you much control over how they’re presented. Do you have any suggestions about the best way to add this functionality such that users have a bit more control over the extra fields formatting … perhaps one more optional parameter that allows a user to specify a comma-separated list of CSS styles for each field (then put each field into it’s own span tags with that class)?
Hi Sam. Unfortunately I’m not that clued up on coding – not yet anyway – so I’m afraid I can’t be of much use. Guess that’s why I rely on plugins so much! Will watch this space eagerly, to see just how obedient you can make WordPress be
Pia, the plugin’s been updated to provide a few new attributes that should address what you need: fields, fields_classes, and fields_make_link. There are some examples in the plugin’s readme.txt file or on the plugin page. Hope that helps you out
Guys, this is exactly what I was looking for. The problem is that I suck at css and I would like to format the output. With the existent classes, I cannot make breaks between the data that is in the output. For example
* Some test2009-09-03 14:35:21Hello there
I wanted it to be:
Some test
2009-09-03 14:35:21
Hello there
Without the bullet and inside a nice little box.
How can I do that?
Thanks in advance for any help
Never mind guys, I just found the fantastic property display:block;
Thank you very much for your plugin. It’s amazing. It would be nice to be able to remove the bullets.
Thanks again,
Dinis
Hey Dinis, thanks for the kind words, I’m glad you found the plugin useful. You got it with “display: block;” for putting each span on it’s own line. Removing bullets from a list isn’t difficult either. Have a peek at this link for an example.
Nice little plugin. But im tryin to figure out on how to include the excerpt in the query… any chance of a little help =)
@jonas: You should be able to add “post_excerpt” to the “fields” attribute, something like this:
[get_posts tag="my favorite tag" fields="post_title, post_date, post_excerpt" fields_classes="class1, class2, class3" fields_make_link="true, false, false"]There’s more documentation on the wordpress.org extension page that will help explain this.
Sam. Great Plugin! I was searching for something like this for days. I juts tried adding the post_excerpt to the fields attribute and excerpts aren’t being displayed. Here’s the code I’m using. Any ideas?
[get_posts tag="maria-sharapova" fields="post_title, post_excerpt" fields_classes="class1, class2" fields_make_link="true, false"]http://journalstone.com/test/
@Journalstone: Glad the plugin is what you need. The excerpts should work. Initially I thought “excerpt” referred to the text before a post’s insert more tag, but found that it actually refers to the separate field directly under a post editing page’s rich text editor, the field that’s called “Excerpt”. Sorry if you already checked that, but figured I’d mention it since it got me despite seeming obvious.
If you’ve already tried filling in that field and still aren’t having any luck, please let me know what version of WP and the plugin you’re using.
Hello there!
I would like to know if there’s a way to add a thumbnail close to the post name using one of the available fields option.
Thank you!
@Sherdan: Are you thinking of using a URL from a custom field for the thumbnail? There’s no way to make that happen with the plugin in it’s current state, but I’ll add it to the feature to-do list.
Hey guys, thanks for this wonderfull job!
Just a thing:
When i use more than one parameter (like title and date), on the page it appears “all together”, without spaces between then.
I’ve just make a modification on the code and it seems to be resolved. I’ll put the hole code updated down here. =)
Thanks again!
========================
’10′,
‘offset’ => ”,
‘category’ => ”,
‘category_name’ => ”,
‘tag’ => ”,
‘orderby’ => ‘date’,
‘order’ => ”,
‘include’ => ”,
‘exclude’ => ”,
‘meta_key’ => ”,
‘meta_value’ => ”,
‘post_type’ => ”,
‘post_status’ => ”,
‘post_parent’ => ”,
‘nopaging’ => ”,
‘ul_class’ => ‘get_posts_class’,
‘fields’ => ‘post_title’,
‘fields_classes’ => ‘post_title_class’,
‘fields_make_link’ => ‘true’),
$args );
$fields_list = explode(“,”, $r['fields']);
$fields_classes_list = explode(“,”, $r['fields_classes']);
$fields_make_link_list = explode(“,”, $r['fields_make_link']);
$content = “\n\n\n”;
$posts = get_posts($args);
foreach( $posts as $post ) {
$content = $content. ” “;
$i = 0;
foreach ( $fields_list as $field ) {
if (isset($fields_classes_list[$i])) {
$content = $content .
” “;
}
if (isset($fields_make_link_list[$i]) &&
($fields_make_link_list[$i] == “true” ||
$fields_make_link_list[$i] == 1)) {
$content = $content .
“ID) .
“\”> “;
}
$field = trim($field);
$content = $content . $post->$field;
if (isset($fields_make_link_list[$i]) &&
($fields_make_link_list[$i] == “true” ||
$fields_make_link_list[$i] == 1)) {
$content = $content . “ “;
}
if (isset($fields_classes_list[$i])) {
$content = $content . ” “;
}
$i++;
}
$content = $content . ” \n”;
}
$content = $content.’ ‘;
return $content;
}
add_shortcode(‘get_posts’, ‘get_posts_generate’);
?>
ow damit, it looks like there was a problem putting the code on this box. I’ll send the modifications by email.
Thanks again.
Could you send me the modified code that formats the posts correctly please? I love this plugin but hate having to put a single line break before each post in order to get this plugin to do what I want it to.
Thanks Sam for your quick answer. Your plugin seemed to be the only one able to show thumbs. Yesterday I’ve printed all the plugin code, trying to figure out how to do that, comparing it with wordpress codex documentation. This is my need:
I have a page dedicated to a city in which I’ll love to show related arguments on the bottom of the page in a fashinating way, like that:
_______________________________________________________
|IMAGE| – LINKED POST TITLE – LINKED POST TAG (only 1)|
| | – EXCERPT or POST CONTENT………………. |
|THUMB| ……………………………………. |
=======================================================
I hope this quite clear for you. I’ve traveled around the world looking for that but unsuccessful. I really hope you’ve got the touch.
Thank you very much indeed for your work I really appreciated.
Have a nice day!
@ DiRaOl
I’ve found a way to solve your problem:
[get_posts tag="your tag="post_title, . ,post_date, . ,post_excerpt"]
put a space, a dot, another space, a comma between arguments.
It Works!
Guy’s I’m in a need of a code which display the latest post including the content.Please help me to do this.
Hi Sam – a belated thank you very much!
Thanks for the plugin, this is the closest I’ve been able to get to what I want to do. What I’d like to produce is a static page that looks something like this: http://ourblocks.net/tag/community-stories
Can Get Posts do that?
@Sherdan: Awesome, thanks for the clever way to add characters. I was thinking of updating the plugin “officially” to allow separator characters (instead of forcing users to use CSS to implement their own separators), but your workaround allows me to delay a bit
I’m still not clear on your request though. Where will the URL for your thumbnail come from? I’m guessing a custom field associated with the post, but want to make sure that’s what you have in mind.
@Mahendran.K: You just need to put the post_content field into your list of what you want to display (you can use any of the fields from the wp_posts database table).
@Leo Romero: I think you could pull most of that info with get_posts, with the exception of a list of tags for a particular post, however I’m not sure if you could format it perfectly even with CSS (I thinking of creating a title that links to your destination page, but there are probably other little obstacles that would crop up after digging into the page). I would recommend creating a new template page and using the same template tag that this plugin is based on for that level of flexibility.
This is an awesome plugin you have here. I’m using wordpress as a cms for a client’s website (so that they will be able to take it over for themselves) and this plug in will go a long way towards making my job even easier.
My only gripe would be that the date field doesn’t get formatted to match what the user has specified in their settings.
As far as all the fields getting spat out onto a single line, a super quick and easy fix is changing lines 69 and 93 from span tags to div tags. Then they get pushed to a new line when I name a class for the field.
Great Plugin just what I needed – Thank you and the comments were very helpful as well.
if you change this code:
$field = trim($field);
$content = $content . $post->$field;
to this code:
if (trim($fields_list[$i])=='post_date'){
$field = trim($field);
$dateFormat = 'l F j, Y';
$content .= mysql2date($dateFormat, $post->$field);
}else{
$field = trim($field);
$content = $content . $post->$field;
}
then you can reformat the date however you like.
Thanks Sam, I’ll try that
@Journalstone
Keeping in mind that I’m no “expert” on WP, I just had a thought while reading your comment regarding `displaying the post excerpt`. You can always display the ‘excerpt field’ of a post in the way that Sam had shown you using the tag get_posts:
[get_posts tag="maria-sharapova" fields="post_title, post_excerpt" fields_classes="class1, class2" fields_make_link="true, false"]
..or you can display the_excerpt() as I do using get_posts:
Read about the WP template tag the_excerpt()
This way will allow you to display the WP first words of the posts.
Note: I also use the following function added to my functions.php file to make what I would call “pretty excerpts”:
function improved_trim_excerpt($text) {
global $post;
if ( ” == $text ) {
$text = get_the_content(”);
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$text = preg_replace(‘@]*?>.*?@si’, ”, $text);
$text = strip_tags($text, ”);
$excerpt_length = 80;
$words = explode(‘ ‘, $text, $excerpt_length + 1);
if (count($words)> $excerpt_length) {
array_pop($words);
array_push($words, ‘{….}’);
$text = implode(‘ ‘, $words);
}
}
return $text;
You can read more about this function here: Improving the WP template tag the_excerpt()
Hope this helps someone. Cheers!
great plugin, it is really what i need, thanks
i have a smalll problem, i am using qTranslate, and seems like Get posts and qTranslate are not friends.
the page shows me all the languages together, how can i resolve this?
thank you
Hi Sam, great plugin – works a treat, however, like Sherdan I am looking to display a thumb with each post. Each post I have has a thumb attached via a url, can this be incorporated into the plugin? I’ve tried coding it myself with no success. Any help would be much appreciated. Thanks
Hey, great plugin. Exactly what I was looking for.
I’m wondering if there’s a way to exclude by date. For instance, I only wants posts to show up that were written after August 2009. I’ve been trying to figure this out on my own in the codex — no luck. Still a beginner with some of this stuff.
Is there any easy way to do this?
Here’s the plugin in action on one of my pages (at the bottom)
http://www.thesnowjunkies.com/ski-area-guide/vermont-ski-areas/killington/
Brilliant plugin thank you.
Great plugin but “Houston, We have a Problem”…
If I use the IntenseDebate Comment Template, every page and post allows comments, even if I have told WordPress to not allow Comments on a particular Page or Post.
The only workaround I have found is to use the WordPress Comment Template, but that definitely isn’t what I want to do.
Thanks in advance for your assistance!
Cheers, Rob
I am using it, but it only shows the last 5, not 10 posts in that category
How can I change the # of posts. I would like to have as many as 20 or 30 show up.
Thanks for all the comments, and sorry for the slow replies from this end …
Vic: Check out the numberposts attribute, that should do the trick.
Robert: Sorry, I’ve never used IntenseDebate, so I’m not able to provide any thoughts there. Hopefully somebody else tuning in can provide some ideas.
Keith & Sherdan: I’m sure adding a thumb is possible, but it’s not something I’m planning on working on for a bit. But, if either of you can gin up a solution, I’d be grateful and would throw it in the next version.
Matt & WuLongTi: Thanks for the updates! I’ll test them out and get them in the next version.
Gravity: You’re welcome, thanks for the kind words
Chip: The easy way might be to order by date, then just show a certain number of posts. Does that get you close to what you want to do? BTW, nice site … I used to live in Burlington, VT for a couple of years, so that post you linked to made me smile.
I am trying to display just the post excerpt of posts with the tag brightspark. This is the code I am using and what displays is the title only. What am I doing wrong?
[get_posts tag="brightspark" numberposts=1 fields="post_excerpt"]
If I have to I don’t mind displaying the the post title & post excerpt.
Hi,
I would also enjoy some way to bring an img http link into the results, if that was stored in a custom field (the name of which I supplied in the get_posts call, ideally).
Also, I can’t work out how to reformat the comment_count field to display as an integer.
This probably relates to me having no idea what a ‘class’ is. Perhaps your opening doco at the top of the page could provide a few basic pointers on simple questions like that (even if they referred to tutorials or whatnot like you had with the css for tables).
I am sorry to probably repeat what others have solved…but where do you change the css to list-style-type: none;? The plugin folder has only php and txt files… should I change it in the static page html?
Thanks again.
Hi Sam,
Great plugin. It does exactly what I need, but I’ve got one question: is there a way to format the output?
For each of my post, I’ve got a custom field called thumb_home_img, and the value is the name of a little picture in jpg. So, I would like to replace the default links’s list given by the plugin with a list of those pics. Possible or not?
Thanks for your answer (and sorry for my poor english ^^; )
Hi – My website is on WP 2.6.8 and Thesis Theme. When I try to use this plugin, it ignores the “tag” attribute. For example, if I use this syntax:
[get_posts tag="about_us"]
the result is null output. However, if I just call [get_posts] by itself, then the result is a list of the titles of the five most recent posts.
My goal was to use this to store snippets of text in private posts and then use them where needed as reusable content, but I don’t think this plugin is going to let me do that.
Thanks,
Tim
Can somebody help me with the “display: block;” and removing bullets as mentioned by Dinis Quelhas above. I am not sure where to add all this code? I am trying to add the plugin to a static page…should I insert the CSS code to the html of the page or should it be inserted somewhere else?
Thanks!!
Ok I am kind of lost here. Is there a php function I can use to get my latest post that I can insert into my static page? I thought that was the point of this plug-in. When I put [get_posts] directly into my html it doesn’t recognize it as anything.
First off this is a fantastic plugin that has made my life so much easier!
Does anyone know of a way to limit the number of characters shown in the post title?
Cheers!
Thanks for the great plugin. I have a blog with several different categories. Some categories have over 250 post and I am noticing that the get post will not display more than 250 post. Is there a way around this issue? I want to have a static page and the page needs to dispaly all the categories with the get post hyperlink to the post which supports unlimited post.
Thanks for your time,
Chad
Hey Sam,
thanks for that great plugin. Really a good idea and very useful.
I hope you continue to work on more featured even though it’s great already. I’d like to see some thumbnails-stuff to include pictures in post lists. This could be done withouth custom fields. Probably you’d want to have a look at “thumbnails for excerpts” plugin which does grab the first image in a post and uses TimThumb to autocreate a neat size-flexible thumb. That would be really great stuff.
Keep up the good work!
Frank
Thanks for this great plugin. I’m using it on my website and it’s brilliant. There is one thing I would like to ask about. Is it possible to get the plugin to only display posts if two values of “tag” are met. For example would it be possible to only display posts if the tags “apple” and “orange” are met? Something a bit like this example below:
[get_posts tag=”apple, orange”… but not display tag=”apple” or tag=”orange”..
Regards
Simon
Thanks for this plugin. It was exactly what i needed. I’ve been using it and it was working until now. I needed to switch to Php 5 to have something else on my site work….and all the other plugins seem to still work except this one. It gives me an error
Fatal error: Cannot access empty property in get-posts.php on line 80
which refers to this line
$content = $content . $post->$field;
If i switch back to php4 it works perfectly, but then of course my other stuff won’t. I was just wondering if you might know how to fix this or work around it.
Thanks
Sam
Still not sure how to get the different fields to display on different lines?
Thanks -Dave