Im trying to make a plugin where i need to create an AJAX search. Im using the method described in http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/ to perform the ajax calls. So far all good.

Problem is not sure how to retrieve the search results in my ajax function.

First attempt was using WP_Query like these:

$query= new WP_Query( array( 'post_type'=> 'any','post_status' => 'publish','posts_per_page' => '8','s' => wp_specialchars($string)) );

Problem here is as soon as i pass the string to "s" the query changes from post_type => 'any to post_type = 'post' which is no good for my purposes. If i dont pass the 's' everything works fine.

Then i also tried by creating my own query like this:

$pageposts = $wpdb->get_results($wpdb->prepare("
            SELECT wp_posts.* 
            FROM wp_posts
            WHERE 1=1 aND wp_posts.post_type IN  ('post', 'page', 'attachment')
            AND (wp_posts.post_status = 'publish') 
            AND (((wp_posts.post_title LIKE %s) OR (wp_posts.post_content LIKE %s)))
            ORDER BY wp_posts.post_date DESC LIMIT 0, 8

But i just keep getting:

<div id='error'>
            <p class='wpdberror'><strong>WordPress database error:</strong> []<br />
            <code>SELECT option_value FROM wp_options WHERE option_name = &#039;aiosp_post_title_format&#039; LIMIT 1</code></p>

There is any proper way to do what im trying? MY plugin consist on a simple form that should show ten titles => hiperlink for a given ajax search.

Thanks All for the help!

1 Answer 1


The error seams to come from another source, not your query. You don't use wp_options table. So maybe skipping

$wpdb->show_errors(); $wpdb->print_error();

would make things even work. But probably there is an error somewhere else.

To get an overview how to create AJAX search helper, you can check here: http://www.wpcodelab.com/archives/create-a-custom-ajax-search-for-wordpress

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.