Best way to get message info in variables without displaying them in Wordpress

I'm writing a Wordpress plugin and you need to go through several posts, grab the data from them (mostly title, permalink and content) and apply processing to them without displaying them on the page.

What I was looking at:

I looked at get_posts () for getting 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 have already been applied, so I am getting the exact data that will be displayed to the user. Each of the above functions seems to apply all necessary filters and do some post processing already, which is great.

Problem:

The problem is that all of these features, at least in WP 2.7.1 (the latest released version right now), by default just dismiss everything and don't even return anything back. the_title () actually maintains a flag that says it doesn't print and doesn't return instead, like

the_title(null, null, false)

      

The other 2, however, do not have such flags and this kind of inconsistency shocks me very much.

I've looked at what each of the the_ () functions do and is trying to output this code so that I can call it without displaying the data (this is a hack in my book, since the behavior of the __ () functions can change at any time). This worked for permalink, but somehow get_the_content () returns NULL. In my opinion, there must be a better way anyway.

So what's the best way to pull these values ​​out without printing them?

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>";
}

      

PS I also looked at What is the best method to create your own Wordpress loops? and while it deals with an already obvious way of looping through messages, the solution there just prints that data.

UPDATE: I have opened a Wordpress ticket about this. http://core.trac.wordpress.org/ticket/9868

+1


a source to share


4 answers


Ok, now everything is sorted. Here is the final result, for those interested:

  • Each post can be accessed by iterating through the array returned by get_posts (), but this data will only be what is in the database, without going through intermediate filters.
  • The preferred way is to access the data using the get_the_ functions and wrap it by calling apply_filters () with the appropriate filter. This will apply all intermediate filters.

apply_filters('the_permalink', get_permalink())



  • the reason it get_the_content()

    returned an empty string is because a special call needs to be made first setup_postdata($post);

    . Then get_the_content()

    returns the data correctly

Thanks everyone for the suggestions.

+1


a source


Most of the_stuff () functions in WP that repeat something have their get_the_stuff () that return something.



Eg get_the_title()

, get_permalink()

...

+4


a source


If you cannot find an exact way to do this, you can always use output buffering.

<?php
ob_start();
echo "World";
$world = ob_get_clean();
echo "Hello $world";
?>

      

+2


a source


Is there a reason why you cannot do your processing while each individual post is posted or when it is displayed?

WP plugins usually run on one column at a time, so there are many hooks for doing things.

0


a source







All Articles