WordPress- list all posts like in editor on ‘insert/edit link’

I am writing a plugin and I want to create a list of posts/and pages the user can choose from that is exactly like the list you see when you click the ‘link’ button in the editor and twirl down ‘Or link to existing content’- does anyone know where I can find/how to call the function that generates that?

(I have asked this in the wordpress.stackexchange but I never seem to get anywhere posting there 🙁 )

Related posts

Leave a Reply

3 comments

  1. Thanks Hobo! Actually I just found my own solution and was just about to post it when I saw your answer!

    Anyway, I’ll post my solution here as it may be useful for someone 😉

    So this is the function I made (basically I nicked it from class_wp_editor.php- its virtually the same as the ‘wp_link_query’ in there…)

    function ican_edit_link_query( $args = array() ) {
        $pts = get_post_types( array( 'public' => true ), 'objects' );
        $pt_names = array_keys( $pts );
    
        $query = array(
            'post_type' => $pt_names,
            'suppress_filters' => true,
            'update_post_term_cache' => false,
            'update_post_meta_cache' => false,
            'post_status' => 'publish',
            'order' => 'ASC',
            'orderby' => 'title',
            'posts_per_page' => 200000,
        );
    
        $args['pagenum'] = isset( $args['pagenum'] ) ? absint( $args['pagenum'] ) : 1;
    
        if ( isset( $args['s'] ) )
            $query['s'] = $args['s'];
    
        $query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;
    
        // Do main query.
        $get_posts = new WP_Query;
        $posts = $get_posts->query( $query );
        // Check if any posts were found.
        if ( ! $get_posts->post_count )
            return false;
    
        // Build results.
        $results = array();
        foreach ( $posts as $post ) {
            if ( 'post' == $post->post_type )
                $info = mysql2date( __( 'Y/m/d' ), $post->post_date );
            else
                $info = $pts[ $post->post_type ]->labels->singular_name;
    
            $results[] = array(
                'ID' => $post->ID,
                'title' => trim( esc_html( strip_tags( get_the_title( $post ) ) ) ),
                'permalink' => get_permalink( $post->ID ),
                'info' => $info,
            );
        }
    

    }

    I made it have a massive number of posts so that all of them are returned, I then iterate over the results array and stuff them into a table.

    then I found a fab bit of code on code canyon that filters the table as you type in a search box- just like in the back end. This is here http://codecanyon.net/item/advanced-tables/53366

    I am not sure if thats the ‘right’ way to do it since obviously I am repeating code- probably you can just run that function with your own args, and then just deal with the results from your own function…I’ll try it out and post some streamlined code…

  2. That list is populated using a call to admin-ajax.php with a parameter of wp-link-ajax. That in turn calls _WP_Editors::wp_link_query() (in wp-includes/class-wp-editor.php).

    Looking at the code, it only seems to accept a page number and/or a search term – you can’t change (for example) the number of posts returned (there are no filter hooks called). Not sure if that’ll be a problem for you.

    To answer your “how can I find the function that generated that”, I guess there are multiple ways, but I used the Tamper Data Firefox plugin. I watched the calls the browser was making to the server (without tampering with anything) and seeing which call updated the list.