WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] First letter posts (5 posts)

  1. Matt_NY
    Member
    Posted 4 years ago #

    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.

  2. sparky157
    Member
    Posted 4 years ago #

    Did you have any luck with this?

  3. MichaelH
    Member
    Posted 4 years ago #

    <?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().
    }
    ?>
  4. x00374
    Member
    Posted 4 years ago #

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

  5. jt70
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic