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 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
Most functions the_stuff() in WP that echo something have their get_the_stuff() counterpart that returns something.
Eg
get_the_title()
,get_permalink()
…If you can’t find the exact way to do it, you can always use output buffering.
OK, I got it all sorted now. Here is the final outcome, for whoever is interested:
apply_filters('the_permalink', get_permalink())
get_the_content()
was returning an empty string is that apparently a special call tosetup_postdata($post);
needs to be done first. Thenget_the_content()
returns data properlyThanks everyone for suggestions.
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.