WordPress.org

Support

Support » Plugins and Hacks » Query All Posts That Start With Letter "A"

Query All Posts That Start With Letter "A"

  • alanchrishughes
    Member

    @alanchrishughes

    anyway to do this? Then query all posts that start with the letter B, all posts that start with the letter C, etc….

Viewing 4 replies - 1 through 4 (of 4 total)
  • Ken Dirschl
    Participant

    @badfun

    It CAN be done, but not by setting it as a query, so the logic is a bit different. You can get the first letter of every post from the database, and then set up conditionals to only display posts with that letter. Works the same way as a query.

    alanchrishughes
    Member

    @alanchrishughes

    How would you go about doing that?

    Is this to put into an index (as in a book index, not index.php)?

    If so, it might be more efficient to run a single query (using the WP query_posts function), fetch all the posts ordered by title and then split them up within PHP.

    Ken Dirschl
    Participant

    @badfun

    You can use apply filters to get the first letter of each post title from the database:

    $first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));

    Depends on how you want to use this. In my case I created a page that has a drop down menu where you can choose the letter. If no letter is chosen, all the posts display. If ‘all’ is chosen, then all the posts display. If a letter is chosen, then just those posts display. Works nicely.

    I can pastebin the code if you are interested.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Query All Posts That Start With Letter "A"’ is closed to new replies.