Print page content with formatting when doing custom SQL query

You need to run the post_content filters. It is a one line change from your current code.

This…

echo $footerElement->post_content;

… should be …

echo apply_filters('the_content',$footerElement->post_content);

That will give you all the same formatting as with normal posts.

You can of course pick and choose which filters you want to run, instead of running them all, and you may want to do that in case a plugin adds something to the post content via a filter (for example, a G+ button). Take a look at the default filters for the_content and check the Codex for the functions of the various callbacks.

To elaborate on that last idea, filter callbacks are basically functions that take input and return a (usually) modified version of that input. That means you can pretty much call them directly if you need to. They are functions like any other. For example…

Take this filter from default-filters.php :

add_filter( 'the_content', 'wptexturize'            );

The callback function is wptexturize. If you wanted to run that “filter” and only that filter you could write:

echo wptexturize($footerElement->post_content);

I can’t swear that will work with every filter out there, since sometimes people do oddball things with filters, but it should work with most– especially the core filters.

Another thing you can do is remove particular filters if you know which ones you don’t want to run. It could be easier than typing out the ones you do want to run.

remove_filter( 'the_content', 'wptexturize' );
// some code maybe
echo apply_filters('the_content',$footerElement->post_content);
// more code maybe
// add the filter back in case it is used later in the page
add_filter('the_content','wptexturize');