Last active
January 19, 2023 10:12
-
-
Save rozklad/6aca1c96a966e78ff1f2 to your computer and use it in GitHub Desktop.
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 | |
// Configuration | |
$input['letter'] = 'A'; | |
# $input['letter'] = '0-9'; // for numbers | |
// Basic arguments for WP query | |
$args = array( | |
'post_type' => 'post' | |
); | |
// Search by first letter | |
if ( isset($input['letter']) ) { | |
global $wpdb; | |
if ( $input['letter'] == '0-9' ) { | |
$query = " | |
SELECT ID | |
FROM $wpdb->posts | |
WHERE SUBSTR($wpdb->posts.post_title,1,1) IN ('".implode("','", range(0,9)) ."') | |
ORDER BY $wpdb->posts.post_title" ; | |
$postids = $wpdb->get_col( $query ); | |
} else { | |
$postids = $wpdb->get_col( $wpdb->prepare(" | |
SELECT ID | |
FROM $wpdb->posts | |
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s | |
ORDER BY $wpdb->posts.post_title",$input['letter'])); | |
} | |
$args['post__in'] = $postids; | |
} | |
// END - Search by first letter | |
$query = new WP_Query($args); | |
// Print out results | |
while ($query->have_posts()): | |
$query->the_post(); | |
// Print out data | |
the_title(); | |
endwhile; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment