SQL query not working in alphabetical post title/content search

Dude, no need of the SQL mess! WP allows you to search through the post content or titles alphabetically using the search parameter.

So, use this solution instead…

global $wpdb;
$q = $_REQUEST['q'];
$posts = get_posts(array('s' => $q, 'post_type' => 'question', 'posts_per_page' => -1));

echo '<ul>';
foreach($posts as $post){
    echo '<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
}
echo '</ul>';

Put this in the AJAX handler function. It will work!

Leave a Comment

tech