Skip to content

Instantly share code, notes, and snippets.

@skydriver
Created April 18, 2014 12:13
Show Gist options
  • Select an option

  • Save skydriver/11040886 to your computer and use it in GitHub Desktop.

Select an option

Save skydriver/11040886 to your computer and use it in GitHub Desktop.
WordPress List Posts by First Title Letter
<?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();
}
?>
@faroukgen42

Copy link
Copy Markdown

This code solve my life, Thank you very match!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment