Skip to content

Instantly share code, notes, and snippets.

@rosswintle
Created October 9, 2013 11:12
Show Gist options
  • Save rosswintle/6899635 to your computer and use it in GitHub Desktop.
Save rosswintle/6899635 to your computer and use it in GitHub Desktop.
Code to do a WordPress query for posts that start with a given letter. Assumes a letter has been passed in the URL as "?s=a" where a is the letter. This code is part of some code that handles ajax requests. You'll need to write mytheme_ajax_list() to run a proper query and/or return results somehow.
// AJAX function for listing directory entries alphabetically
add_action('wp_ajax_directory_alpha', 'mytheme_ajax_directory_alpha');
add_action('wp_ajax_nopriv_directory_alpha', 'mytheme_ajax_directory_alpha');
function mytheme_ajax_directory_alpha() {
function alpha_post_where( $where ) {
$new_where = $where . " AND lower(substring(post_title, 1, 1)) = lower('" . $_REQUEST['s'] . "')";
return $new_where;
}
add_filter( 'posts_where', 'alpha_post_where' );
mytheme_ajax_list();
die;
}
@outbravedigital
Copy link

Looks great! will have a play with this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment