• Resolved VierasTalo

    (@vierastalo)


    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]

Viewing 12 replies - 31 through 42 (of 42 total)
  • i think i would let evan work on this. Its creating unnecessary confusion.
    @evan the array is empty after index item 10

    Thread Starter VierasTalo

    (@vierastalo)

    @evan I added the print after the split. Here are the results:
    Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => Long Goodbye, The (1973) ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => )

    @bkdimri thank you for all your hard work!

    What about the $film_names variable??

    If the film_names_array is not populated, then something else may be going wrong, as $film_names_array = preg_split( '/\r\n|\r|\n/', $film_names ); should be splitting your text area field into an array.

    print_r( $film_names );

    Thread Starter VierasTalo

    (@vierastalo)

    I added the print for film_names and the results are:
    Long Goodbye, The (1973)

    There must be another issue at hand here, as $film_names should contain more than one entry.

    WP_Query was being used. Can you try swapping that out for get_posts().

    $posts = new get_posts( array(
        'numberposts' => -1,
        'meta_key' => 'elokuvat_l',
        'order'=> 'ASC'
    ) );
    
    if( $posts ) {
    	echo '<ul>';
    		$temporary_list = array();
    		foreach( $posts as $post ) {
    			$film_names = get_field( 'elokuvat_l', $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 $post_id => $film_name ) {
    			echo '<li><a href="' . get_permalink($film_name['post_id']) . '">' . $film_name['film_name'] . '</a></li>';
    		}
    	echo '</ul>';
    }

    Thread Starter VierasTalo

    (@vierastalo)

    Initially nothing showed on the page, but I removed the new from $posts = new get_posts( array( and now it sure shows movies. They are not in any order, though. On the good side, I left the print_r( $temporary_list ) at the end and it totally has everything as it should, however, they aren’t in alphabetical order at the moment as you can see at https://www.laajakuva.com/elokuvahakemisto/l/

    Ok, so that’s a little progress. So now, let’s try re-adding the multi-sort function back in to ensure they get sorted properly.

    Let me take a look at a test install on my end and see what I can come up with so we don’t have to go back and forth testing random things.

    Evan

    Hey VierasTalo,

    Sorry about the break. I had to head home from work for the day.

    At home though, I was able to put together a working bit of code for you. Give the following a try:

    $posts = new get_posts( array(
        'numberposts' => -1,
        'meta_key' => 'elokuvat_l',
        'order'=> 'ASC'
    ) );
    
    if( $posts ) {
    	echo '<ul>';
    		$temporary_list = array();
    		foreach( $posts as $post ) {
    			$film_names = get_field( 'elokuvat_l', $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 );
    			}
    		}
    		// custom sort function
    		usort( $temporary_list, function ( $a, $b ) {
    			return strcasecmp($a['film_name'], $b['film_name']); // compare two strings ignoring case
    		});
    		// loop and display
    		foreach( $temporary_list as $film_name ){
    			echo '<li><a href="' . get_permalink($film_name['post_id']) . '">' . $film_name['film_name'] . '</a></li>';
    		}
    	echo '</ul>';
    }

    I was able to put together a test array, and tested out that custom sort function – which seems to be working well.

    Thread Starter VierasTalo

    (@vierastalo)

    Evan, I can’t thank you enough! The list is now fully functional: https://www.laajakuva.com/elokuvahakemisto/l/ I’ll implement it to the other pages after getting some sleep since it’s 1AM out here.

    Seriously, thank you so much for the continued struggle. Keep on rocking!

    No problem at all, I’m glad that we we’re able to get things resolved. Sorry it took so long, and so much back and forth!

    Have a great rest of your night!
    Evan

    great help evan

Viewing 12 replies - 31 through 42 (of 42 total)
  • The topic ‘Alphabetizing a custom loop’ is closed to new replies.