I am trying to prepare an array with Custom Post Type posts to populate a select options in TinyMCE.

I was thinking to fetch posts, get their IDs and titles and put them all inside $data variable, which I would like to send to TinyMCE with wp_localize_script. The problem with this code is that it doesn't load. It seems like I can't call WP_Query nor use if(myquery->have_posts() or while(myquery->have_posts()), the page takes too long to complete and I get Allowed memory size of 134217728 bytes exhausted. Currently I have only 2 posts in CTP for testing and getting data should be fast.

I am doing this by creating a class inside plugin.

public function __construct()       {
        add_action('admin_head', array($this, 'prepare_for_tinymce'));
        //add_action('admin_head', array($this, 'gavickpro_add_my_tc_button'));

    public function prepare_for_tinymce(){

        wp_register_script('literatura_reference_js', plugins_url('tinymce.js', __FILE__ ), array( ), 1.0, true );

        // get custom data from WP_QUERY - THIS DOESN'T WORK
        $data = $this->pl_get_books();

    /*     $data = array(   array(
    *           'id' => 1,
    *           'title' => 'asfsdf'
    *           ) );

        // send data to javascript - THIS WORKS OK (I have tried with $data array above)
        wp_localize_script( 'literatura_reference_js', 'literatura_tinymce', $data );


    // get data from custom post type
    public function pl_get_books(){

        $myquery= new WP_Query(
                'post_type' => 'knjige',
                'post_status' => 'publish',
                'posts_per_page' => -1

        $data = array();
        if($myquery->have_posts()) :
            while($myquery->have_posts()) :
                $data[] = array(
                    'id' => get_the_ID(),
                    'title' => get_the_title()
            return $data;


Any ideas what am I missing?

1 Answer 1


Try placing $myquery->the_post(); inside your while loop.

I think get_the_ID() and get_the_title() depend on that being setup first.

Also, wp_reset_query(); should be replaced with wp_reset_postdata();.

  • and while($myquery->have_posts()) will also never be false, it's an infinite loop.
    – Milo
    Jan 26, 2016 at 20:19
  • That solved my problem, although I still couldn't make it work with WP_Query for some reason, but it works with get_posts :)
    – Marko
    Jan 26, 2016 at 20:37

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.