Created
October 14, 2014 18:35
-
-
Save nielsvr/a62ee5851adc4a462396 to your computer and use it in GitHub Desktop.
Get posts starting with a letter
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
<?php | |
add_filter( 'posts_where', 'start_with_letter', 10, 2 ); | |
function start_with_letter( $where, &$wp_query ) | |
{ | |
global $wpdb; | |
if ( $start_with_letter = $wp_query->get( 'start_with_letter' ) ) { | |
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( like_escape( $start_with_letter ) ) . '%\''; | |
} | |
return $where; | |
} | |
$posts = new WP_Query( | |
array( | |
"start_with_letter" => "a" | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment