Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Are you trying to alphabetically search the posts of the current logged in user? –  Rutwick Gangurde Jun 21 '12 at 7:46
    
I will modify the $post, add POST_AUTHOR = ... –  Jenny Jun 21 '12 at 7:50
    
@RutwickGangurde , I just edited my question. I want to modify he line of $post, add post_author in this querry. –  Jenny Jun 21 '12 at 7:59
    
But if you're trying to do what I'm assuming you are, then it can be done in an easier way! –  Rutwick Gangurde Jun 21 '12 at 8:05
    
@RutwickGangurde , How? as long as the user can search within his own posts, it will be great. –  Jenny Jun 21 '12 at 8:12

2 Answers 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!

share|improve this answer
    
sorry for my question not clear. I need to know how to hook up with the wp_ajax_find_post function to add my own thing. It's not necessary to modify the search. I just edited my question. –  Jenny Jun 21 '12 at 10:20
    
You mean you want to call it via jQuery? –  Rutwick Gangurde Jun 21 '12 at 10:28
    
yes, wp_ajax_find_posts is originally wp ajax function. It works with jQuery. Everything is in place, I only want to find a way to replace or filter the $post in this function. –  Jenny Jun 21 '12 at 11:09

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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