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:
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 http://stackoverflow.com/questions/570152/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