WordPress plugin : workaround to get excerpt of recent posts & only published messages

07 / Aug / 2009 by Amit Jain 6 comments

While integrating the blogs using wordpress plugin in one of the project I am working on. I observed that using current version(0.1) of wordpress plugin, when we request for recent posts, it returns not only published blogs but also blogs with status as draft(which are not yet published). Along with that, we needed to show excerpt of recent blogs. So the workaround I found is given below

//create a class blog
public class Blog{
    String title
    String description
    String link
}

/** A function that returns the requested number of recent published posts as  Blog objects list. Its parameters :
     * @param count : number of recent blogs needed.
     * @param textSize : number of charaters to be displayed in a description
     * @param blogsCount : it is used to make a recursive call, incase wordpress service returned draft posts as well.   Pass this value equivalent to count
     * @return List : Returns list of blog objects.
     */

  List getRecentExcerptedPosts(int count, int textSize, int blogsCount = count){

         List excerptPosts = []
         Blog blog
         int postsAdded=0
          try {
               //As blogs requested from wordpress may have draft posts, so we requested twice the number of blogs needed as this request to wordpress plugin is expensive.
               wordpressService.getRecentPosts(blogsCount*2).each { post ->
                   if(postsAdded < count){
                        if(post.post_status.equalsIgnoreCase('publish')){
                            blog = new Blog()
                            blog.title = post.title
                            blog.description = post.description.replaceAll("\\<.*?\\>","") //removes html code from description
                            blog.description = textSize > blog.description.length()?blog.description : blog.description.substring(0, textSize)
                            blog.link = post.permaLink
                            excerptPosts << blog
                            postsAdded++
                       }
                   }
               }
               if(postsAdded<count){
                   //makes the recursive call
                   excerptPosts= getRecentExcerptedPosts(count,textSize, blogsCount*2)
               }
            } catch (Exception e) {
                log.error "Error rendering the wordpress recentPosts tag", e
            }
            return excerptPosts
       }
 

Cheers,
~~Amit Jain~~
amit@intelligrape.com
http://www.tothenew.com/blog/

FOUND THIS USEFUL? SHARE IT

comments (6)

  1. free wordpress themes

    WordPress SecureSuite Fix potential flaws in wordpress that hackers may take advantage of! We focus on 19 key foundations for total 99.99% security satisfaction! One Click PC Software To secure all your WordPress Sites Sleep easy at night knowing hackers have been stopped dead in their tracks!

    Reply
  2. Luis Arias

    Hi Amit,

    I just released version 0.1.1 of the plugin which should work for you without any changes to your code. I finally didn’t use your workaround as is since I wanted to also take the opportunity to provide caching for recent posts. Please let me know if this works for you.

    It would be great to have your help in making the plugin better. I have actually just started using it myself in app, so its bound to need more and better features !!

    Luis

    Reply
  3. amit

    Thanks Luis!

    I am looking forward for the same. I will feel honored, if I can also contribute in your efforts to make this wordpress plugin more useful.

    Regards

    Reply
  4. Luis Arias

    I just posted a JIRA issue on this here . Unfortunately the WP xml rpc interface is not designed to accept filtering on a posts status, so effectively your logic will have to be implemented in the plugin.

    Reply
  5. Luis Arias

    Wow thanks for posting this ! (Thanks G. Laforge for pointing this out). I will be fixing this and updating the plugin ASAP.

    Luis

    Reply

Leave a Reply to free wordpress themes Cancel reply

Your email address will not be published. Required fields are marked *