I ran a custom loop the other day to get some twitter information. Since I was running the code in a template and that template was being called from another loop, I was able to get the template data by doing this

 $content = $template->post_content;
 echo $content;

I figured this would be better than setting up another loop. When I did this, I found that the content was unformatted. So doing a little research, I found that if I pass it through the content filter

  $content = $template->post_content; 
  $content = apply_filters( 'the_content', $content); 
  echo $content;

tha solved my problem. So now I am using this function to get pages on my homepage and found that it does the same thing. It brings back the page unformatted

 function posts_and_pages_on_home( $query ) {
  if ( $query->is_main_query() ) {
    $query->set( 'post_type', array( 'post', 'page' ) );
    return $query;
 add_action( 'pre_get_posts', 'posts_and_pages_on_home' );

So naturally, I figured that if I pass $query through the same content filter that it would fix my issue. Nope, I was wrong. Is there a way to get the function to filter the return $query through the_content filter?

  • Do you have a debugging tool like FirePHP? Today I saw ChromePHP too. You'll be able to inspect your objects along the way of the code. – brasofilo Feb 2 '13 at 3:47
  • what's your home template code? The loop would be enaught... – david.binda Feb 2 '13 at 7:11
  • I installed a plugin called "Debug This" is works pretty well to show me object data. I am still learning how to use it and how to debug code in this manner. – Jamie Feb 2 '13 at 13:46

The pre_get_posts action hook only allows you to modify the $query object .

Any formatting or output of data is done in the template file within the loop. When you are in the loop you have access to various template tags that output the post data formatted (filters applied).

if ( have_posts() ) : while ( have_posts() ) : the_post();

    the_content(); //Same as echo apply_filters( 'the_content', $post->post_content );
  • my shortcodes don't get processed though and template files don't load. That is why I was wondering if something else needed to be done. – Jamie Feb 2 '13 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.