Created
October 9, 2013 11:12
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks great! will have a play with this :)