Best way to get post info into variables without displaying them in WordPress

I am writing a WordPress plugin and need to go through a number of posts, grab the data from them (for the most part title, permalink, and content), and apply processing to them without displaying them on the page.

What I’ve looked at:

Read More

I’ve looked at get_posts() for getting the posts, and then

getting title via the_title(),
content via the_content(),
and permalink via the_permalink()

Keep in mind that I need this data after all filters had already been applied, so that I get the exact data that would be displayed to the user. Each of the functions above seems to apply all necessary filters and do some postprocessing already, which is great.

The Problem:

The problem is all these functions, at least in WP 2.7.1 (latest released version right now) by default just echo everything and don’t even return anything back. the_title() actually supports a flag that says do not print and return instead, like so

the_title(null, null, false)

The other 2, however, don’t have such flags, and such inconsistency is quite shocking to me.

I’ve looked at what each of the_() functions does and tried to pull this code out so that I can call it without displaying the data (this is a hack in my book, as the behavior of the_() functions can change at any time). This worked for permalink but for some reason get_the_content() returns NULL. There has to be a better way anyway, I believe.

So, what is the best way to pull out these values without printing them?

Some sample code

global $post;
$posts = get_posts(array('numberposts' => $limit));

foreach($posts as $post){
    $title = the_title(null, null, false); // the_title() actually supports a "do not print" flag
    $permalink = apply_filters('the_permalink', get_permalink()); // thanks, WP, for being so consistent in your functions - the_permalink() just prints /s
    $content = apply_filters('the_content', get_the_content()); // this doesn't even work - get_the_content() returns NULL for me
    print "<a href='$permalink'>$title</a><br>";
    print htmlentities($content, ENT_COMPAT, "UTF-8"). "<br>";
}

P.S. I’ve also looked at What is the best method for creating your own WordPress loops? and while it deals with an already obvious way to cycle through posts, the solution there just prints this data.

UPDATE: I’ve opened a ticket with WordPress about this. http://core.trac.wordpress.org/ticket/9868

Related posts

Leave a Reply

4 comments

  1. Most functions the_stuff() in WP that echo something have their get_the_stuff() counterpart that returns something.

    Eg get_the_title(), get_permalink()

  2. If you can’t find the exact way to do it, you can always use output buffering.

    <?php
    ob_start();
    echo "World";
    $world = ob_get_clean();
    echo "Hello $world";
    ?>
    
  3. OK, I got it all sorted now. Here is the final outcome, for whoever is interested:

    • Each post’s data can be accessed via iterating through the array returned by get_posts(), but this data will just be whatever is in the database, without passing through any intermediate filters
    • The preferred way is to access data using get_the_ functions and them wrapping them in an call to apply_filters() with the appropriate filter. This way, all intermediate filters will be applied.

    apply_filters('the_permalink', get_permalink())

    • the reason why get_the_content() was returning an empty string is that apparently a special call to setup_postdata($post); needs to be done first. Then get_the_content() returns data properly

    Thanks everyone for suggestions.

  4. Is there any reason you can’t do your processing at the time each individual post is posted, or when it’s being displayed?

    WP plugins generally work on a single post at a time, so there are plenty of hooks for doing things that way.