WordPress.org

Forums

Query All Posts That Start With Letter "A" (5 posts)

  1. alanchrishughes
    Member
    Posted 4 years ago #

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

  2. Ken Dirschl
    Member
    Posted 4 years ago #

    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.

  3. alanchrishughes
    Member
    Posted 4 years ago #

    How would you go about doing that?

  4. Damian Gostomski
    Member
    Posted 4 years ago #

    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.

  5. Ken Dirschl
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.