Support » Fixing WordPress » Sort posts alphabetically by tag?

  • My Posts Page pulls in all of the posts, which I have setup not to blog, but to post Artist Profiles.

    Every time a new post is created it contains a title, an image & paragraph, a meta tag that contains the Bio, and a Tag that contains Artist’s last name.

    I am trying to pull in the Posts and have them appear alphabetically by Tag. Is this possible? Can someone point me in the right direction?

    I tried using this code:

    <?php if (have_posts()) : ?>
                    <?php $posts = query_posts( $query_string . '&orderby=tag&order=asc' ); ?>
    		<?php while (have_posts()) : the_post(); ?>

    edit: thanks in advance!

Viewing 15 replies - 1 through 15 (of 16 total)
  • There currently isn’t support for tag as an “orderby” parameter…

    The alternative would be to loop through the posts as normal but store them in array and resort the array, then loop over the new array… (in theory, i’m thinking out loud)..

    Maybe like:

    <?php
    $alltags = get_terms('post_tag');
    if ($alltags){
    //echo "<pre>"; print_r($alltags); echo "</pre>";
      foreach( $alltags as $tag ) {
        $args=array(
          'tag__in' => array($tag->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts in tag '.$tag->name;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    thanks t31os and MichaelH.

    I tried your code MichaelH, and it sort of worked, but not quite.

    Here is a link to how it looks now:
    http://gallerysevenmaynard.com/gamma/artists/

    Any ideas?

    Alternatively, I am open to suggestions of a better way to do this that would take advantage of WordPress’ built in operations.

    Is it easier to sort posts by custom field? Could I create a custom field “ArtistLastName” and not have that data posted, but use it to sort the posts?

    The query_posts() article discusses the orderby=meta_value if you use custom fields.

    MichaelH,
    Thanks for pointing me to that article.

    I think I grasp and after creating a Custom Field called ArtistLastName and adding each Artist’s last name as the Value, here is what I have crafted for the template:

    <?php $posts = query_posts('meta_key=ArtistLastName&order=ASC');  ?>
    
    	<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    			<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    
    				<div class="entry">
                    	<h2 class="artist"><?php the_title(); ?></h2>
                       <div class="alignright art">
                        <?php the_content();?></div>

    And it does SOMETHING but what and why I can’t figure out.

    Without the query_posts code, it displays the artists in this order (by date posted):
    Hurd
    Forbes
    Kaufman
    Bunis
    Flisher

    With the query_posts code, it just reverses the order of artists, instead of sorting them alphabetically.

    Any ideas?

    Okay I just revisited the query_posts Article and thought I might fix it by adding orderby=meta_value, so now my code looks exactly as it did above, except this line:
    <?php $posts = query_posts('meta_key=ArtistLastName&order=ASC'); ?>
    has changed to this line:
    <?php $posts = query_posts('orderby=meta_value&meta_key=ArtistLastName&order=ASC'); ?>

    and now this is the order of display:

    • Flisher
    • Forbes
    • Bunis
    • Hurd
    • Kaufman

    hmmmmm…
    Any ideas?

    Use new WP_Query like my example above.

    MichaelH, I am not sure what you mean. I apologize but I am still in the beginning stages of understanding php.

    Can you walk me a few more steps in the right direction? I am very interested in working out this solution as best I can on my own, but a few more steps would be really helpful.

    Any chance?

    I think this is what you are looking for:

    <?php
    $args=array(
      'orderby' => 'meta_value',
      'meta_key' => 'ArtistLastName',
      'order' => 'ASC',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    I’m messing something up =/

    http://gallerysevenmaynard.com/gamma/artists/

    <?php
    $args=array(
      'orderby' => 'meta_value',
      'meta_key' => 'ArtistLastName',
      'order' => 'ASC',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    	<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    			<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    
    				<div class="entry">
                    	<h2 class="artist"><?php the_title(); ?></h2>
                       <div class="alignright art">
                        <?php the_content();?></div>
    
    					<div class="bio"><?	echo get_post_meta($post->ID, Bio, true);?></div>
    
                        <div class="clear" style="margin-bottom:60px;"></div>
    				</div>
    </div>

    Not sure of all your divs etc but wouldn’t it be something like:

    <?php
    $args=array(
      'orderby' => 'meta_value',
      'meta_key' => 'ArtistLastName',
      'order' => 'ASC',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
       			<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    
    				<div class="entry">
                    	<h2 class="artist"><?php the_title(); ?></h2>
                       <div class="alignright art">
                        <?php the_content();?></div>
    
    					<div class="bio"><?	echo get_post_meta($post->ID, Bio, true);?></div>
    
                        <div class="clear" style="margin-bottom:60px;"></div>
    				</div>
    </div>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Michael – Thanks so much for continuing to return to this thread.

    I am about to try the latest code you provided.

    (More importantly) I am trying to figure out how and why it works.

    The array values all make sense to me except the last 2:

    'posts_per_page' => -1,
      'caller_get_posts'=> 1

    Can you please explain what that says and does?

    😉

    (michael it worked thank you!)

    return all posts
    'posts_per_page' => -1,

    don’t include stickies
    'caller_get_posts'=> 1

    See query_posts() for more info.

    Ahh, got it. Thank you so much.

    What I is for 5 posts to show on the main /artists page, so I set posts_per_page to 5, and that worked!

    But, where do the other (6 for now) posts go?

    Ideally, there would be a link at the bottom of every 5 posts that links to the next group of 5. Is this possible?

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Sort posts alphabetically by tag?’ is closed to new replies.