WordPress.org

Ready to get started?Download WordPress

Forums

What argument to use in query_posts to get posts with given letter? (27 posts)

  1. klevismiho
    Member
    Posted 2 years ago #

    How to get for example all posts that start with "A"?

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Put this in your functions.php:

    add_filter('query_vars', 'letter_queryvar' );
    function letter_queryvar( $qvars )
    {
      $qvars[] = 'letter';
      return $qvars;
    }
    
    add_filter('posts_where', 'letter_where' );
    function letter_where( $where )
    {
        global $wp_query;
        global $wpdb;
    
        if( isset( $wp_query->query_vars['letter'] )) {
    
           $where .= " AND $wpdb->posts.post_title LIKE '".$wp_query->query_vars['letter']."%'";
        }
        return $where;
    }

    Now You can query posts like this:

    query_posts('letter=A');
  3. keesiemeijer
    moderator
    Posted 2 years ago #

    The code returns Posts with titles that begins with the letter A.
    If you want Posts where the post content begins with the letter A change this:

    $where .= " AND $wpdb->posts.post_title LIKE '".$wp_query->query_vars['letter']."%' ";

    to this:

    $where .= " AND $wpdb->posts.post_content LIKE '".$wp_query->query_vars['letter']."%' ";
  4. klevismiho
    Member
    Posted 2 years ago #

    Thank you, thank you, but It doesn't work.

  5. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with the code from my last post (just above yours).

  6. klevismiho
    Member
    Posted 2 years ago #

    Sorry, my question was not right. I want to get all posts whose titles begin with a specific letter, so basically the code that you gave me first.
    But doesn't work.

  7. keesiemeijer
    moderator
    Posted 2 years ago #

    Does it return any posts or none when you query the loop?

    For testing try putting this after query_posts.

    query_posts('letter=A');
    global $wp_query;
    echo '<pre>';
    print_r($wp_query);
    echo '</pre>';

    Can you see if "letter" is in the "query_vars" Array.

    Can you paste and submit the full code of the template file where you query the loop into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

  8. klevismiho
    Member
    Posted 2 years ago #

    Here's what the output of print_r is:

    WP_Query Object
    (
    [query_vars] => Array
    (
    [letter] => A
    .....

  9. klevismiho
    Member
    Posted 2 years ago #

    The code in my template is:

    <?php
    query_posts('letter=A');
    while (have_posts()) : the_post();
     echo 'test';
    endwhile;
    ?>
  10. keesiemeijer
    moderator
    Posted 2 years ago #

    try that with:

    <?php
    query_posts('letter=A');
    while (have_posts()) : the_post();
     the_title();
    endwhile;
    ?>
  11. klevismiho
    Member
    Posted 2 years ago #

    No, nothing :(

  12. keesiemeijer
    moderator
    Posted 2 years ago #

    So it returns nothing?
    You do have post titles that start with an A?

    Very strange because this works on my site.

  13. klevismiho
    Member
    Posted 2 years ago #

    Yeah nothing. I have only 2 posts that start with an A (Atmosphere, Amber).
    If I put query_posts('category_name=test'), it gets the posts from test category.

  14. klevismiho
    Member
    Posted 2 years ago #

    I have wordpress 3.2.1 by the way.

  15. klevismiho
    Member
    Posted 2 years ago #

    Maybe you have a older version.

  16. keesiemeijer
    moderator
    Posted 2 years ago #

    No, I have the same WordPress version.

    Are there multiple loops in your template file? Can you put the full template file in a pastebin.

  17. klevismiho
    Member
    Posted 2 years ago #

  18. klevismiho
    Member
    Posted 2 years ago #

    I've found a solution: http://pastebin.com/DXduNtKk

    I know that your solution is better than this, but what can I do :)

  19. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

  20. keesiemeijer
    moderator
    Posted 2 years ago #

    Well, if that works, your solution is better. Maybe your theme or a plugin is conflicting with my code.

    From the print_r array can you show me the [request].

  21. klevismiho
    Member
    Posted 2 years ago #

    Thank you alchymyth, but the solution posted there didn't work.

    keesiemeijer here is the print_r: http://pastebin.com/VvAdyVHs

  22. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    i tested both the solution posted by @keesiemeijer in his first reply, as well the one by @MichaelH, and both worked in my local test blog.

    i therefore support the idea, that: 'Maybe your theme or a plugin is conflicting with the code.'

  23. klevismiho
    Member
    Posted 2 years ago #

    Yes, I think so too. I'm using the twentyten theme by the way.

  24. keesiemeijer
    moderator
    Posted 2 years ago #

    Tested it with twentyten and it works.
    try:
    - deactivating all plugins to see if this resolves the problem? If this works, re-activate the plugins one by one until you find the problematic plugin(s).

  25. klevismiho
    Member
    Posted 2 years ago #

    I tried it keesiemeijer, doesn't work, but don't worry because I'm using the other solution.

    Thank you for your support.

  26. keesiemeijer
    moderator
    Posted 2 years ago #

    No problem. Glad you found a solution to your problem.

  27. klevismiho
    Member
    Posted 2 years ago #

    As I said, your solution looks better, mine is just a workaround.

Topic Closed

This topic has been closed to new replies.

About this Topic