-
-
Save thinhbg59/b2ff4f522b2352d3cc19b873f71f93e2 to your computer and use it in GitHub Desktop.
WordPress List Posts by First Title Letter
This file contains hidden or 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 | |
global $wpdb; | |
$first_char = esc_attr($_GET[$search_key]); | |
$postids = $wpdb->get_col($wpdb->prepare(" | |
SELECT ID | |
FROM $wpdb->posts | |
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s | |
AND $wpdb->posts.post_type = 'product' | |
ORDER BY $wpdb->posts.post_title",$first_char)); | |
if ( $postids ) { | |
$args = array( | |
'post__in' => $postids, | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'caller_get_posts'=> 1 | |
); | |
$my_query = null; | |
$my_query = new WP_Query($args); | |
if( $my_query->have_posts() ) { | |
echo '<p>List of Posts Titles beginning with the letter <strong>'. $first_char . '<strong></p>'; | |
$counter = 1; | |
while ($my_query->have_posts()) : $my_query->the_post(); ?> | |
<p> | |
<span><?php echo $counter++; ?></span> | |
<a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> | |
</p> | |
<?php endwhile; | |
} | |
wp_reset_query(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment