• Resolved ddsuresh

    (@ddsuresh)


    I am building a page where it is displaying A-Z index

    https://www.orkidz.co.in/rhymes

    While clicking on link A, it should display rhymes that starts with A in specific category. I tried below query but it does not work for me.

    query_posts(array('category_name' => 'xyz', 'orderby' => 'title', 'order' => 'ASC', 'title' => <strong>$index . '%'</strong>))

    https://www.orkidz.co.in/rhymes/?index=a must display rhymes starts with A
    https://www.orkidz.co.in/rhymes/?index=b must display rhymes starts with B

    Right now it is retrieving posts properly but i have used if statement to filter out. But i don’t like that if statement because it is looping through complete A – Z to filter out title that starts with A.

    Anybody can help me out for better solution?

Viewing 1 replies (of 1 total)
  • You can do this with a filter. I can’t test this, but it should be close. Add this function either to your functions.php or to the template:

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

    Then, code your query like this:

    $mam_global_where = " AND strtoupper(substr($wpdb->posts.post_title,0,1)) = strtoupper($index)";
    query_posts(array('category_name' => 'xyz', 'orderby' => 'title', 'order' => 'ASC'))
    $mam_global_where = false;
Viewing 1 replies (of 1 total)
  • The topic ‘Querying posts based on index in a page’ is closed to new replies.