WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to make a php script which, given a post id, returns post content. I'm not the one who writes articles, I'm just operating on a wp site. Post content is for an app of mine which has to parse it.

My script is very simple:

           $post_id = $_GET['id'];
           $queried_post = get_post($post_id);
           echo $queried_post->post_content;

Result is the article but sometimes the image is embedded in a [caption] field, like this:

[caption id="attachment_78971" align="alignnone" width="276"]<a href="href" rel="attachment wp-att-78971"><img class="size-medium wp-image-78971"/></a>[/caption]

other times I have just the image

<img ...ecc 

why is that? Is there a way to force the same output regardless of who has written the article?

share|improve this question
up vote 2 down vote accepted

The [caption] part shows up because one of your authors/editors added a caption to the image when it was inserted into the post. You will have this problem not only with captions but with any other shortcodes that your authors/editors use. And you should be aware that WordPress includes several in its Core but various plugins and even themes provide a lot of them. You will need to process the shortcode:

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

Or strip the shortcodes:

echo strip_shortcodes( $queried_post->post_content );

Which you do depends on the results that you want.

You should also be aware that echoing raw post content-- ie. $queried_post->post_content-- will probably not give you the formatting that you expect, because raw post content is not typically complete markup. Running the the_content filter, as above, sorts out most of that.

share|improve this answer
thank you so much! – user1610075 Nov 2 '12 at 19:02

That’s a shortcode, and you should run …

apply_filters( 'the_content', $queried_post->post_content );

… before you print it out.

You cannot prevent users from using that from your script. And you should not.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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