Skip to content

Instantly share code, notes, and snippets.

@nielsvr
Created October 14, 2014 18:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nielsvr/a62ee5851adc4a462396 to your computer and use it in GitHub Desktop.
Save nielsvr/a62ee5851adc4a462396 to your computer and use it in GitHub Desktop.
Get posts starting with a letter
<?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