Alphabetizing a custom loop
-
OK so uh. Here’s a thing. I’m currently using Advanced Custom Fields to generate post-specific values for each article in our database of over 450 of them. ACF gives me a field underneath the post in the WYSIWYG-view, and we can enter in a film name there. Then we compile these values, sorted by first letter, to pages. The problem is that due to the way we have ACF set up, if there are multiple films with the same first letter in a given post, those names will have to be entered into one text area, one row per film. Thus, when we call up the ACF-values, it’ll give us a solid block of film titles rather than one row being it’s own value.
Now, I figured that out with the help of the ACF-community. Now the page code splits these more-than-1-row -values into their own subsets. The problem now becomes that for whatever reason, the alphabetical order of the code isn’t working. It alphabetizes it until it reaches these multiple value -articles, wherein it splits the values, but then instead of sorting those values to alphabetical order with the rest of them on the page, it just throws them one after another. You can see the results on this page, for example:
Le Miracle de Ailes (1961)
Laukaus tehtaalla (1973)This is the code currently being used to generate this data. In this example page, elokuvat_c is the value that was put in when writing the article. Where did we go wrong?
<?php get_header(); ?> <section class="content"> <?php get_template_part('inc/page-title'); ?> <div class="pad group"> <div class="entry"> <div class="entry-inner"> <?php the_content(); ?> <?php wp_link_pages(array('before'=>'<div class="post-pages">'.__('Pages:','hueman'),'after'=>'</div>')); ?> </div> <div class="clear"></div> </div><!--/.entry--> <?php $posts = get_posts(array( 'numberposts' => -1, 'meta_key' => 'elokuvat_c', 'order'=> 'ASC', 'orderby' => 'elokuvat_c' )); if($posts) { echo ' <ul>'; $temporary_list = array(); foreach($posts as $post) { $film_names = get_field('elokuvat_c', $post->ID); $film_names_array = preg_split( '/\r\n|\r|\n/', $film_names ); foreach($film_names_array as $film_name){ $temporary_list[] = array('post_id' => $post->ID, 'film_name' => $film_name); } } foreach( $temporary_list as $i => $row ) { $order[ $i ] = $row['film_name']; } array_multisort( $order, $temporary_list ); foreach($temporary_list as $film_name){ echo ' <li><a href="' . get_permalink($film_name['post_id']) . '">' . $film_name['film_name'] . '</a></li> '; } echo '</ul> '; } ?> </div><!--/.pad--> </section><!--/.content--> <?php get_sidebar(); ?> <?php get_footer(); ?>
Thanks for any help!
The page I need help with: [log in to see the link]
- The topic ‘Alphabetizing a custom loop’ is closed to new replies.