WordPress.org

Ready to get started?Download WordPress

Forums

Sorting second level of custom fields (8 posts)

  1. moegly
    Member
    Posted 1 year ago #

    I'm developing a site right now that has several custom fields including Artist and Album. I currently have the posts sorting by Artist name, but I would then like to having secondary sorting by Album name as well. For example, 1 Artist could have 5 Albums listed, so I want the posts to be sorted by Artist name first, and then have their Albums sorted alphabetically as well. Here is my current code:

    $args=array(
    'post_type' => 'albums',
    'order' => 'ASC',
    'meta_key' => 'custom_meta_artist',
    'orderby' => 'meta_value',
    'posts_per_page' => -1,
    );

    And that works great for sorting the Artists alphabetically, but if they have 5 albums, those albums have no order. Any suggestions?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    You will need to use a filter to alter the sort order.

    Try adding these lines before your $args setting:

    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
       return $orderby;
    }
    add_filter('posts_orderby','mam_posts_orderby');
    $mam_global_orderby = "$wpdb->postmeta.meta_value ASC, $wpdb->posts.post_title ASC";

    Then add this line after the query_posts() to clear the filter:

    $mam_global_orderby = '';
  3. moegly
    Member
    Posted 1 year ago #

    Thanks a lot for the response vtxyzzy! Unfortunately I was not able to get this working. I'm not extremely proficient in PHP, so I apologize for being naive. I placed the first bit of code you gave me above $args=array( and then placed $mam_global_orderby = ''; after my query_posts call, but it didn't seem to filter my other custom fields in ascending order. I'm probably just doing something wrong, but here is a larger section of my code if this helps:

    <?php
    
    $last_char = '';
    $args=array(
      'post_type' => 'albums',
      'order' => 'ASC',
      'meta_key' => 'custom_meta_artist',
      'orderby' => 'meta_value',
      'posts_per_page' => -1,
    );
    
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo '';
      while ($my_query->have_posts()) : $my_query->the_post();
        $this_char = strtoupper(substr(get_post_meta($post->ID, 'custom_meta_artist', true),0,1));
        if ($this_char != $last_char) {
          $last_char = $this_char;
          echo '<h2>'.$last_char.'</h2>';} ?>
    
    		<?php
    			$custom_meta = get_post_custom($post->ID);
    			$artist = $custom_meta['custom_meta_artist'][0];
    			$album = $custom_meta['custom_meta_album'][0];
    			$size = $custom_meta['custom_meta_size'][0];
    			$color = $custom_meta['custom_meta_color'][0];
    			$label = $custom_meta['custom_meta_label'][0];
    			$pressing = $custom_meta['custom_meta_pressing'][0];
    		?>

    I then call those custom fields in a table

    <td class="Artist"><?php echo $artist; ?></td>
    				<td class="Album"><?php echo $album; ?></td>
    				<td class="Size"><?php echo $size; ?></td>
    				<td class="Color"><?php echo $color; ?></td>
    				<td class="Label"><?php echo $label; ?></td>
    				<td class="Pressing"><?php echo $pressing; ?></td>

    Could you help me with where the code you created will go?

  4. vtxyzzy
    Member
    Posted 1 year ago #

    Actually the code I gave was incomplete. And, you only asked for sorting a second level in the original post. Sorting on 6 meta_keys is an entirely different matter, and much more complicated.

    I will have to work on a solution and get back to you later.

  5. moegly
    Member
    Posted 1 year ago #

    I really don't need to sort all 6, I just want the posts to sort by Artist alphabetically, and then if there are multiple posts with the same Artist, the Albums would then sort alphabetically as well. I really appreciate your help, and if there are any articles you think would be helpful for me to figure it out myself let me know.

    Thanks,
    Nick

  6. vtxyzzy
    Member
    Posted 1 year ago #

    Here is an article with sample code to do the sort:
    http://wordpress.mcdspot.com/2012/10/24/sort-posts-on-multiple-custom-fields/

    Note that you will need to change the $meta_keys array to your own keys, and the $args array for your post_type.

    Also, in your code you used the 'brace' form of if ( $my_query->have_posts() ) {, but the 'colon' form of while ($my_query->have_posts()) :.

    It is not officially correct to mix these forms although it will often work. You should use a consistent form.

  7. moegly
    Member
    Posted 1 year ago #

    I put in your code but I couldn't get it to work. I'm sure it's something on my end, but I don't understand most of your code. Here's what I have:

    <?php
    // Test sorting on 6 meta_keys
    //
    // Strategy is to get all posts with the custom_meta_artist key
    // then loop through the posts getting all other keys and building an array
    // with the ID and sort key.  Then sort the full posts on the ID array.
    //
    $meta_keys = array('artist','album','size','color','label','pressing');
    $args = array(
       'post_type' => 'albums',
       'meta_key' => $meta_keys[0],
       'orderby' => 'meta_value',
       'posts_per_page' => -1,
       'ignore_sticky_posts' => 1,
       );
    $my_query = new WP_Query($args);
     
    // print_r($my_query);
     
    if( $my_query->have_posts() ) :
       foreach ( $my_query->posts as $one_post ) {
          $id = $one_post->ID;
          $custom_meta = get_post_custom($id);
          $id_array["$id"] = array();
          $id_array["$id"]['sort_key'] = '';
          foreach ( $meta_keys as $key ) {
             $id_array["$id"]['sort_key'] .= $custom_meta[$key][0] . ' ';
             $id_array["$id"][$key] = $custom_meta[$key][0];
          }
       }
     
       //print_r('<p>ID ARRAY:');print_r($id_array);print_r('</p>');
     
       // Sort posts array on the $id_array sort_key.
       function sort_6_keys($a, $b) {
          global $id_array;
          // print_r("AID:{$a->ID} SORT_KEY: {$id_array[$a->ID]['sort_key']}");
          // print_r("BID:{$b->ID} SORT_KEY: {$id_array[$b->ID]['sort_key']}");
     
          return strcmp( $id_array[$a->ID]['sort_key'],
             $id_array[$b->ID]['sort_key'] );
       }
     
       // print_r('<p>BEFORE SORT:');print_r($my_query->posts);print_r('</p>');
     
       usort($my_query->posts, 'sort_6_keys');
     
       // print_r('<p>AFTER SORT:');print_r($my_query->posts);print_r('</p>');
     
       while ( $my_query->have_posts() ) : $my_query->the_post();
          $id = $post->ID;
          $artist = $id_array[$id]['custom_meta_artist'[0]];
          $album = $id_array[$id]['custom_meta_album'[1]];
          $size = $id_array[$id]['custom_meta_size'[2]];
          $color = $id_array[$id]['custom_meta_color'[3]];
          $label = $id_array[$id]['custom_meta_label'[4]];
          $pressing = $id_array[$id]['custom_meta_pressing'[5]];
          echo "$artist | $album | $size | $color | $label | $pressing <br />";
      
    
    				<table id="Table"><tr>
    				<td class="Thumb">
    				<a class="fancybox" rel="group" href="<?php
    $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
    foreach ( $attachments as $attachment_id => $attachment ) {
    	echo wp_get_attachment_url( $attachment_id, 'medium' );
    } ?>">
    				<?php 
    
    				if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
      				the_post_thumbnail( array(21,21) );
    				}
    				?>
    				</a>
    				</td>
    				<td class="Artist"><?php echo $artist; ?></td>
    				<td class="Album"><?php echo $album; ?></td>
    				<td class="Size"><?php echo $size; ?></td>
    				<td class="Color"><?php echo $color; ?></td>
    				<td class="Label"><?php echo $label; ?></td>
    				<td class="Pressing"><?php echo $pressing; ?></td>
    				</tr></table>
    
    				<div style="height:5px"></div>
    
    				<?php
      endwhile;
    } //if ($my_query)
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  8. vtxyzzy
    Member
    Posted 1 year ago #

    Sorry for the delay in replying, I have been away for a while.

    It appears that you have unnecessary quote marks in these lines:

    $artist = $id_array[$id]['custom_meta_artist'[0]];
          $album = $id_array[$id]['custom_meta_album'[1]];
          $size = $id_array[$id]['custom_meta_size'[2]];
          $color = $id_array[$id]['custom_meta_color'[3]];
          $label = $id_array[$id]['custom_meta_label'[4]];
          $pressing = $id_array[$id]['custom_meta_pressing'[5]];

    Please try removing them, like this:

    $artist = $id_array[$id][$meta_keys[0]];
          $album = $id_array[$id][$meta_keys[1]];
          $size = $id_array[$id][$meta_keys[2]];
          $color = $id_array[$id][$meta_keys[3]];
          $label = $id_array[$id][$meta_keys[4]];
          $pressing = $id_array[$id][$meta_keys[5]];

    EDIT: I changed the code under 'Please try removing them'.

Topic Closed

This topic has been closed to new replies.

About this Topic