WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How use the LIKE in query_posts (8 posts)

  1. lucaswxp
    Member
    Posted 3 years ago #

    Hello everybody!
    I'm having a problem.
    I want to set the LIKE in query_posts, something like that:

    query_posts(array('like' => 'something%', 'posts_per_page' => 5))

    How can I do this?
    Bye o/

  2. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    There is no "like" parameter in query_posts.

    http://codex.wordpress.org/Function_Reference/query_posts

  3. lucaswxp
    Member
    Posted 3 years ago #

    yes, but, I want to know if I can do something with the same effect of "LIKE", because I'm offering a list to my users:
    A - B - C - D - E - F -...
    So... when the user click on "A" for exemple, will list posts that start with this letter. I thought to use de the $wpdb->get_results(), but that way I will have to do the pagination manually... (using LIMIT and OFFSET)

  4. vtxyzzy
    Member
    Posted 3 years ago #

    You can use a filter to add a condition to the where clause of the query. I don't know the variable that holds the chosen letter, but assume it is $letter. Use this code just ahead of your query_posts:

    <?php
    function mam_posts_where ($where) {
       global $mam_global_where;
       if ($mam_global_where) $where .= " $mam_global_where";
       return $where;
    }
    add_filter('posts_where','mam_posts_where');
    $mam_global_where = "AND $wpdb->posts.post_title LIKE '$letter%'";
  5. lucaswxp
    Member
    Posted 3 years ago #

    Perfect!
    Thnkx "vtxyzzy" @.@

  6. vtxyzzy
    Member
    Posted 3 years ago #

    Glad it worked! Now, please use the dropdown at top right to mark this topic 'Resolved'.

  7. alanchrishughes
    Member
    Posted 3 years ago #

    Is there any way to explain this in more universal terms? I'm not sure what the terms of his specific situation are and what I would need to use and what I would need to replace with my own terms.

    But I have a similar situation, I want to organize local bands and people can click on "A" and find a list of all the bands whose name starts with the letter A. Then you can click on "B" to find a list of all the bands that start with the letter B. Etc...

  8. vtxyzzy
    Member
    Posted 3 years ago #

    You should start a new thread with this question. You normally do not get responses to a 'Resolved' thread.

Topic Closed

This topic has been closed to new replies.

About this Topic