0

Is this possible? I am trying to use a pre-defined template for my contents. To do that, I use something like this:

add_filter( 'default_content', 'custom_editor_content' );
function custom_editor_content( $content ) {

     $args = array(
        'posts_per_page'=> 15,   
        'orderby'  => array( 
                'ID' => 'DESC' ,
            ),
    );
    $query = new WP_Query( $args );
    $query_contents=Array();

    while ( $query->have_posts() ) {
        $query->the_post();
        array_push($query_contents,Array(
            "id"=>get_the_ID(),
            "title"=>get_the_title(),
            "url"=>get_permalink(),
        ));

    }
    $content = '



    '.get_the_title( $id ).'


    ';
   return $content;
}

But I can't get the post, somehow. Does someone know how to do this? If I put the while statement into $content, it shows the whole query in the editor and that's not what I want of course.

All I want to do is to fetch the post title and show it in the content editor (after posting or before, that wouldn't matter)

Could someone help me out?

5
  • Are you trying to fetch the title of the post you're creating, and add it to the content? The default_content filter appears to be meant for the initial content in the Write Post form.
    – Pat J
    Commented Nov 21, 2017 at 21:59
  • Yes, I am trying that indeed. I want to fetch the title of the post I am creating... is that even possible?
    – Siyah
    Commented Nov 21, 2017 at 22:01
  • Probably, but I suspect you'll need to involve some Javascript, because PHP won't have access to the data until you've at least saved the post initially. Until you save it, the data (including the title) is not in the database.
    – Pat J
    Commented Nov 22, 2017 at 0:03
  • But what if I'd have a variable that fetches the post title AFTER I create the post. Is that possible with solely PHP? I don't need to see the title until it's created anyway.
    – Siyah
    Commented Nov 22, 2017 at 8:32
  • Yes, you can get the post's title with get_the_title( $post_id ) after the post is saved. But in that case, I don't think you want the default_content filter. I think you're looking instead for the_editor_content, which filters the post's content prior to putting it into the editor.
    – Pat J
    Commented Nov 22, 2017 at 15:06

1 Answer 1

0

$id isn't set anywhere in your code snippet. get_the_title( null ); returns an empty string. If you want the title of a post, you'll need to set $id to a proper post ID.

(Also, $args) is unset in the code you've posted, and I don't see anywhere that you use query_contents either.)

1
  • Hi there, sorry, I missed to add a chunk of my code. I edited it now. Could you help me out? I did set a get_the_ID(). Should I put that in a variable too?
    – Siyah
    Commented Nov 21, 2017 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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