WordPress.org

Ready to get started?Download WordPress

Forums

How to query by post name first letter (2 posts)

  1. dorlowski
    Member
    Posted 4 years ago #

    I'm trying to put together a directory and would like a way to query my posts based on the first letter. I'm guessing I need to use query_posts(). Here is a base SQL snippet:

    name = 'A%'

    In other words I just want to return posts that begin on the letter "A". I could just return everything then use javascript to create and dynamically sort the whole list, but that gets really long and will no doubt tax the webserver and database. Is there another way around this so I can query posts that begin on a certain letter?

    Thanks!

  2. s_ha_dum
    Member
    Posted 4 years ago #

    As far as I'm aware, no. You can't directly query using parameters like that. You could use the $wpdb class to select post IDs where name LIKE 'a%' and then use query_posts with the 'post__in' => array(...IDs...) parameter to get the rest of the data. You might be better off just using $wpdb to grab it all in one go-- depends on what information need from the query I guess.

Topic Closed

This topic has been closed to new replies.

About this Topic