WordPress.org

Ready to get started?Download WordPress

Forums

Sort posts alphabetically by tag? (17 posts)

  1. trulyandy
    Member
    Posted 4 years ago #

    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!

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    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)..

  3. MichaelH
    Member
    Posted 4 years ago #

    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().
    ?>
  4. trulyandy
    Member
    Posted 4 years ago #

    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?

  5. MichaelH
    Member
    Posted 4 years ago #

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

  6. trulyandy
    Member
    Posted 4 years ago #

    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?

  7. trulyandy
    Member
    Posted 4 years ago #

    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?

  8. MichaelH
    Member
    Posted 4 years ago #

    Use new WP_Query like my example above.

  9. trulyandy
    Member
    Posted 4 years ago #

    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?

  10. MichaelH
    Member
    Posted 4 years ago #

    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().
    ?>
  11. trulyandy
    Member
    Posted 4 years ago #

    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>
  12. MichaelH
    Member
    Posted 4 years ago #

    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().
    ?>
  13. trulyandy
    Member
    Posted 4 years ago #

    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?

    ;)

  14. trulyandy
    Member
    Posted 4 years ago #

    (michael it worked thank you!)

  15. MichaelH
    Member
    Posted 4 years ago #

    return all posts
    'posts_per_page' => -1,

    don't include stickies
    'caller_get_posts'=> 1

    See query_posts() for more info.

  16. trulyandy
    Member
    Posted 4 years ago #

    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?

  17. trulyandy
    Member
    Posted 4 years ago #

    <?php next_posts_link('&laquo; Older Entries') ?></div>

    I tried just adding that line in after the `<?php
    endwhile;
    }
    wp_reset_query(); // Restore global post data stomped by the_post().
    ?>`

    It included a link that led to artists/page/2 - which I want, but it just shows the first 5 Artists again (identical to page 1). Any ideas what I would need to add so that each additional page pulls the next 5 from the list, instead of always the same first 5?

Topic Closed

This topic has been closed to new replies.

About this Topic