Support » Plugins » pulling posts alphabetically with headers

  • Resolved nadine00

    (@nadine00)


    Hi,

    I was just wondering how I would go about pulling posts alphabetically but with headers dividing them based on the alphabet?

    So for example:

    A
    -all posts here that start with the letter a

    B
    – all posts here that start with the letter b.

    etc..

    thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • hmm. That really isn’t what I need, I need something I can just put raw into a template page. so, not an archive of post titles that you can individually link too, but a page of all the actual posts.

    Is there a way to maybe do this without a plugin? Maybe multiple calls on a page?

    Something that would fit in a set up kind of like this:

    ‘numberposts=999&orderby=title&order=ASC’

    like a limiter, or range.

    Sorry, if i’m not seeing an obvious thing here. But thanks for your suggestions! 🙂

    <?php
    $last_char = '';
    $args=array(
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'Alphabetic index of all ' . count($my_query->posts) . ' posts';
      while ($my_query->have_posts()) : $my_query->the_post();
        $this_char = strtoupper(substr($post->post_title,0,1));
        if ($this_char != $last_char) {
          $last_char = $this_char;
          echo '<h2>'.$last_char.'</h2>';
        } ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    } //if ($my_query)
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Wow thank you Michael. If I wanted to do this with posts from just one category, rather than all of them, would it be something like this:

    $args=array(
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1
      'in_category'=>3
    );

    or like this:

    $last_char = '';
    $cat = 3;
    $args=array(
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1
      'in_category'=>$cat
    );

    or maybe its something totally different.

    Again, thanks for the query from before. Really appreciate it.

    cheers

    Nadine.

    $last_char = '';
    $cat = 3;
    $args=array(
      'cat' => $cat,
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1
    );

    or for multiple categories

    $last_char = '';
    $args=array(
      'category__in' => array(3,7,45,2),
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1
    );

    bump.

    Sorry to have to bother the forum again, but I find myself in a scenario where I need to sort this, and make the headers, by a custom field rather by post_title (client request).

    This seems to work fine for sorting:

    $last_char = '';
    $args=array(
      'category__in' => array(3,7,45,2),
      'orderby' => 'guest_sort',
      'order' => 'ASC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1
    );

    But I’m not sure how to figure out getting the headers to use the custom field as well.
    I tried this:
    $post->guest_sort

    but that was wrong.

    Then I tried pulling the custom filed value into a variable with get_post_meta but I’m probably doing it wrong:

    $guestSort = get_post_custom_values('guest_sort');
    
    $this_char = strtoupper(substr($guestSort,0,1));

    but that didn’t work. I also tried stringing together a meta_value&meta_key=guest_sort string, but that didn’t work.

    $this_char = strtoupper(substr($post->meta_value&meta_key=guest_sort,0,1));`

    Suggestions?

    Thank you.

    Nadine.

    found it!
    over here: http://wordpress.org/support/topic/248010?replies=22

    Just in case anyone comes back to this thread.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘pulling posts alphabetically with headers’ is closed to new replies.