I try this:

    function get_ct($id){
        $ct = apply_filters('the_content', get_post_field('post_content', $id));
        return $ct;

    $arr = array('post_content' => get_ct(126));

    echo json_encode($arr);

but the post_content 's values ist null any suggestion? thanks

  • apple_filters is expecting a callable/function as a second parameter, but you're passing in a value. Also which file is this running in? – Tom J Nowell Jul 18 '18 at 16:57
  • I think Tom means apply_filters – Brent Jul 18 '18 at 17:07
  • @TomJNowell you're thinking of add_filter. apply_filters is being used more or less correctly in the snippet above. – SeventhSteel Jul 18 '18 at 17:08
  • The code executes for me, I would double-check that your post #126 has post_content data and isn't trashed or set to private. – SeventhSteel Jul 18 '18 at 17:09
  • i need to send data of post content in json format via ajax .. that is the problem – Victor Lozada Jul 18 '18 at 17:23

You can use get_the_content() to fetch the post content, e.g.:

$content = get_the_content( $post_id );

Then you can pass it through the_content filter and json encode

However, based on your comments, it looks like you're creating an endpoint for AJAX calls that returns post content. Don't bother! WordPress comes with one out of the box!



The response has a content field and even contains a rendered version that contains a fully rendered post as you desired.

You don't need to do anything to get this, it's already on your site at /wp-json


this work for me

function postData($id){
    $p_data = get_post($id);
    if ($p_data) {
    $content = $p_data->post_content;
    return $content = apply_filters('the_content', $content);
    else return false;


  • Could you explain why is this the solution for given question? (And what the question really was?) – Krzysiek Dróżdż Jul 18 '18 at 19:45
  • I needed, save the content of a post in one array.. the code above work's header("Content-Type: application/json"); $arr = array('post_content'=>postData(126)); echo json_encode($arr); – Victor Lozada Jul 18 '18 at 21:51
  • based on that comment, you can probably just use the REST API rather than writing your own endpoint, e.g. tomjn.com/wp-json/wp/v2/posts/848 – Tom J Nowell Jul 18 '18 at 22:55

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.