How to modify wp_ajax function?

I want to modify wp_ajax_find_posts function. It receives search string from find-posts form and delivers search results by ajax. Part of the function looks like this:

function wp_ajax_find_posts() {
    global $wpdb;
    check_ajax_referer( 'find-posts' );

    // ........
    $search .= " OR ($wpdb->posts.post_title LIKE '%{$term}%') OR ($wpdb->posts.post_content LIKE '%{$term}%')";
    $posts = $wpdb->get_results( "SELECT ID, post_title, post_status, post_date FROM $wpdb->posts WHERE post_type = '$what' AND post_status IN ('draft', 'publish') AND ($search) ORDER BY post_date_gmt DESC LIMIT 50" );

    $html = //......

    $x = new WP_Ajax_Response();
    $x->add( array(
        'what' => $what,
        'data' => $html
    ));
    $x->send();
}
add_action( 'wp_ajax_find_posts', 'wp_ajax_find_posts,1)

I want to modify the line $post to add post_author to the querry in order to limit the results within current loggin user. I would like to learn how to hook up with this wp_ajax_find_post function to let it accept my modified $post. Do I have to do remove_action and add_action to make completely rewrite of the above funtion?

Related posts

Leave a Reply

2 comments

  1. You can jump in front of an AJAX hook by specifying a higher priority (i.e. lower number), like this:

    add_action( 'wp_ajax_find_posts', 'wp_ajax_find_posts', 0 );
    

    NB: works because 0 < 1

  2. I’ve posted the code for alphabetical title/content search here: SQL query not working in alphabetical post title/content search

    Now you need the author as the currently logged in user. So modify the function to include this:

    //This goes at the top in the function
    global $current_user;
    get_currentuserinfo();
    
    //Change the get_posts in the original code with this
    $posts = get_posts(array('s' => $q, 'author' => $current_user->ID, 'posts_per_page' => 2));
    

    Let me know if it works!