-1
<?php
global $wpdb;
$first_char = 'A';
$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>
You’re showing off like this:


In SQL, simply change from
= 'A'forin ('A', 'B', ..., 'E'), nay?– Woss
@Andersoncarloswoss made a mistake, can make a more concrete example ?
– Johnny
@Andersoncarloswoss can help me ?
– Johnny
In the
WHERE SUBSTR()he’s checking if the first character isAonly. You could get all the database records and handle filtering in PHP. I believe that there will not be a supermassive number of posts to the point of locking the server.– CypherPotato
@Cypherpotato can make me an example?
– Johnny
@Johnny removes the line from the
whereand when iterating the search result, check whether the first letter isa, b.– CypherPotato
@Cypherpotato I did this but it didn’t work, it shows nothing when I change the Where line.
– Johnny
After all you want to simply sort alphabetically or also filter by the initial letter? As on your page you have
List of Posts Titles beginning with the letter A, I imagine you want to make a page for each letter, right? Or would it serve to have all the posts sorted and presented on the same page? And paging, is or is not to have?– tvdias