Viewing 15 replies - 1 through 15 (of 26 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    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');

    Moderator keesiemeijer

    (@keesiemeijer)

    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']."%' ";

    Thread Starter klevismiho

    (@klevismiho)

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

    Moderator keesiemeijer

    (@keesiemeijer)

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

    Thread Starter klevismiho

    (@klevismiho)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    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.

    Thread Starter klevismiho

    (@klevismiho)

    Here’s what the output of print_r is:

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

    Thread Starter klevismiho

    (@klevismiho)

    The code in my template is:

    <?php
    query_posts('letter=A');
    while (have_posts()) : the_post();
     echo 'test';
    endwhile;
    ?>
    Moderator keesiemeijer

    (@keesiemeijer)

    try that with:

    <?php
    query_posts('letter=A');
    while (have_posts()) : the_post();
     the_title();
    endwhile;
    ?>

    Thread Starter klevismiho

    (@klevismiho)

    No, nothing ??

    Moderator keesiemeijer

    (@keesiemeijer)

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

    Very strange because this works on my site.

    Thread Starter klevismiho

    (@klevismiho)

    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.

    Thread Starter klevismiho

    (@klevismiho)

    I have wordpress 3.2.1 by the way.

    Thread Starter klevismiho

    (@klevismiho)

    Maybe you have a older version.

    Moderator keesiemeijer

    (@keesiemeijer)

    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.

Viewing 15 replies - 1 through 15 (of 26 total)
  • The topic ‘What argument to use in query_posts to get posts with given letter?’ is closed to new replies.