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
",array('%'.$string.'%')));
$wpdb->show_errors();
$wpdb->print_error();
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 = 'aiosp_post_title_format' LIMIT 1</code></p>
</div>
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!