WordPress.org

Ready to get started?Download WordPress

Forums

query_posts, name, and like (3 posts)

  1. Lazurus
    Member
    Posted 8 years ago #

    Hi all,

    I'm looking for a way to use LIKE with the query_posts function.

    something like:
    '
    query_posts('category_name="foo"&name LIKE "A*"&orderby=title&order=ASC&posts_per_page=-1''

    Help?

    Thanks!

  2. Hmm. I can't think of an easier way to do this due to that to see what category the post is in, you have to check the posts2cat table and to find matching posts, you have to query the posts table.

    (And yes, I realize using * is icky, but we probably need all columns in the table anyway and I'm too lazy to list all the columns out.)

    $posts = wpdb->get_results('SELECT * FROM $wpdb->posts WHERE post_title LIKE A% ORDER BY post_title');

    foreach ($posts as $post) {
    if (wpdb->query('SELECT rel_id FROM $wpdb->post2cat WHERE category_id = X AND post_id = $post->ID') === 0) continue;
    else {
    echo 'this is a post that starts with "A" and is in cat ID #X';
    }
    }

    (rel_id doesn't matter, it was just picked at random. We just gotta query for something. COUNT would work too.)

    An easier way to do this might to be putting all posts starting with the letter "A" in a category of their own.

  3. Lazurus
    Member
    Posted 8 years ago #

    . . . ugh. But thank you :-)

Topic Closed

This topic has been closed to new replies.

About this Topic