• Resolved matt_ny

    (@matt_ny)


    Hi everyone!

    How can I show posts on specific page, which are starts (post title) on some letter? For example, I want to show posts only started on letter “B”.

    Is it possible to do, without writing this letter in custom fields every time I publish a new post?

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Did you have any luck with this?

    <?php
    //get all post IDs for posts beginning with cap B, in title order,
    //display posts
    $first_char = 'B';
    
    $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",$first_char)); 
    
    if ($postids) {
    $args=array(
      'post__in' => $postids,
      'post_type' => 'post',
      '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 'List of Posts Titles beginning with the letter '. $first_char;
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>

    Thanks for code MichaelH, but where should I post it? in post.php?

    MichaelH,

    Thanks for all the support you give. I’ve seen your name around this forum.

    One question on your code before I try it. How would I go about only showing posts from a specific category. I think I could muddle through with an ‘if’ statement, but wondered if there might be a more efficient way. I’m trying to replace the AZIndex function with something less query intensive.

    Thanks.
    JT70

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘First letter posts’ is closed to new replies.