• 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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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.

    Thread Starter lazurus

    (@lazurus)

    . . . ugh. But thank you ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘query_posts, name, and like’ is closed to new replies.