• 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 15 replies - 16 through 30 (of 42 total)
  • Thread Starter VierasTalo

    (@vierastalo)

    If I add the print_r( $temporary_list ); -line before the array_multisort, like so:

    foreach ($temporary_list as $key => $row) {
        $post_id[$key]  = $row['post_id'];
        $film_name[$key] = $row['film_name'];
    }
        print_r( $temporary_list );
        array_multisort($film_name, SORT_ASC, SORT_STRING);
    
        foreach($temporary_list as $film_name){
            echo '<li><a href="' . get_permalink($film_name['post_id']) . '">' . $film_name['film_name'] . '</a></li>';
        }
    
    	echo '</ul>';
    }

    Then this is what comes up on the page:

    +
    Array ( [0] => Array ( [post_id] => [film_name] => ) [1] => Array ( [post_id] => [film_name] => ) [2] => Array ( [post_id] => [film_name] => ) [3] => Array ( [post_id] => [film_name] => ) [4] => Array ( [post_id] => [film_name] => ) [5] => Array ( [post_id] => [film_name] => ) [6] => Array ( [post_id] => [film_name] => ) [7] => Array ( [post_id] => [film_name] => ) [8] => Array ( [post_id] => [film_name] => ) [9] => Array ( [post_id] => [film_name] => ) [10] => Array ( [post_id] => 2037 [film_name] => La Bête Humaine (1938) ) [11] => Array ( [post_id] => [film_name] => ) [12] => Array ( [post_id] => [film_name] => ) [13] => Array ( [post_id] => [film_name] => ) [14] => Array ( [post_id] => [film_name] => ) [15] => Array ( [post_id] => [film_name] => ) [16] => Array ( [post_id] => [film_name] => ) [17] => Array ( [post_id] => [film_name] => ) [18] => Array ( [post_id] => [film_name] => ) [19] => Array ( [post_id] => [film_name] => ) [20] => Array ( [post_id] => [film_name] => ) [21] => Array ( [post_id] => [film_name] => ) [22] => Array ( [post_id] => [film_name] => ) [23] => Array ( [post_id] => [film_name] => ) [24] => Array ( [post_id] => [film_name] => ) [25] => Array ( [post_id] => [film_name] => ) [26] => Array ( [post_id] => [film_name] => ) [27] => Array ( [post_id] => [film_name] => ) [28] => Array ( [post_id] => [film_name] => ) [29] => Array ( [post_id] => [film_name] => ) [30] => Array ( [post_id] => [film_name] => ) [31] => Array ( [post_id] => [film_name] => ) [32] => Array ( [post_id] => [film_name] => ) [33] => Array ( [post_id] => [film_name] => ) [34] => Array ( [post_id] => [film_name] => ) [35] => Array ( [post_id] => [film_name] => ) [36] => Array ( [post_id] => [film_name] => ) [37] => Array ( [post_id] => [film_name] => ) [38] => Array ( [post_id] => [film_name] => ) [39] => Array ( [post_id] => [film_name] => ) [40] => Array ( [post_id] => [film_name] => ) [41] => Array ( [post_id] => [film_name] => ) [42] => Array ( [post_id] => [film_name] => ) [43] => Array ( [post_id] => [film_name] => ) [44] => Array ( [post_id] => [film_name] => ) [45] => Array ( [post_id] => [film_name] => ) [46] => Array ( [post_id] => [film_name] => ) )
    La Bête Humaine (1938)

    You can see it here: https://www.laajakuva.com/elokuvahakemisto/l/

    so each article has some movies associated. I am not sure , but did you consider tags instead of acf_field?

    That’s odd. It looks like the variables stored in $temporary_list are all empty.

    Using:

    foreach($film_names_array as $film_name){
       $temporary_list[] = array('post_id' => $post->ID, 'film_name' => $film_name );
    }

    as a template, your variables should not be empty.

    sorry cleared this post @evans has alread asked for printing the array

    Thread Starter VierasTalo

    (@vierastalo)

    I replaced the print_r -line with the var_dump and this came out:

    +
    array(47) { [0]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [1]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [2]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [3]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [4]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [5]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [6]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [7]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [8]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [9]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [10]=> array(2) { ["post_id"]=> int(2037) ["film_name"]=> string(23) "La Bête Humaine (1938)" } [11]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [12]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [13]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [14]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [15]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [16]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [17]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [18]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [19]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [20]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [21]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [22]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [23]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [24]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [25]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [26]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [27]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [28]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [29]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [30]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [31]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [32]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [33]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [34]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [35]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [36]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [37]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [38]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [39]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [40]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [41]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [42]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [43]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [44]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [45]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } [46]=> array(2) { ["post_id"]=> NULL ["film_name"]=> string(0) "" } }

    It’s the same page as before if you want to look there instead.

    EDIT: Oh, right.

    So, I would’ve used tags but we already surface them for another purpose for our readers and didn’t want to infer on the user experience. It also can’t be your basic custom variable since it needs to be as user friendly as possible for our not-so-technically-skilled writers, ergo the plug-in.

    so this is what i tried as test code snippet since we dont know what is going on in your wordpress pages

    $temporary_list[] = array('post_id' => '10', 'film_name' => 'La Moivie1');
    $temporary_list[] = array('post_id' => '20', 'film_name' => 'le Movi2');
    $temporary_list[] = array('post_id' => '3', 'film_name' => 'LA movie 3');
    $temporary_list[] = array('post_id' => '5', 'film_name' => 'lu moviw8');
    $temporary_list[] = array('post_id' => '1', 'film_name' => 'li Movie');
    var_dump($temporary_list);
    foreach ($temporary_list as $key => $row) {
        $post_id[$key]  = $row['post_id'];
        $film_name[$key] = $row['film_name'];
    }
    array_multisort($film_name, SORT_ASC, $temporary_list);
    foreach ($temporary_list as $movieName) {
    	echo '<br>',$movieName['film_name'],'|',$movieName['post_id'];
    }

    and it returns

    LA movie 3|3
    La Moivie1|10
    le Movi2|20
    li Movie|1

    as evan mentioned your $temporary_list is empty

    give the following snippet a shot:

    $posts = new WP_Query( 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>';
    }

    I removed a bit of code that I believe was causing some issues.

    sorry .. deleted this .

    Thread Starter VierasTalo

    (@vierastalo)

    Evan, I applied the change, and, well, something happened. I don’t know what, though. The page still shows a single film and a plus sign: https://www.laajakuva.com/elokuvahakemisto/l/

    However, it’s a different film than before.

    Below all of that code can you re-add the print_r( $temporary_list ) to check what the $temporary_list array now contains?

    Thread Starter VierasTalo

    (@vierastalo)

    Sure, it’s there now. Here’s the paste:
    Array ( [0] => Array ( [post_id] => [film_name] => ) [1] => Array ( [post_id] => [film_name] => ) [2] => Array ( [post_id] => [film_name] => ) [3] => Array ( [post_id] => [film_name] => ) [4] => Array ( [post_id] => [film_name] => ) [5] => Array ( [post_id] => [film_name] => ) [6] => Array ( [post_id] => [film_name] => ) [7] => Array ( [post_id] => [film_name] => ) [8] => Array ( [post_id] => [film_name] => ) [9] => Array ( [post_id] => [film_name] => ) [10] => Array ( [post_id] => 21 [film_name] => Long Goodbye, The (1973) ) [11] => Array ( [post_id] => [film_name] => ) [12] => Array ( [post_id] => [film_name] => ) [13] => Array ( [post_id] => [film_name] => ) [14] => Array ( [post_id] => [film_name] => ) [15] => Array ( [post_id] => [film_name] => ) [16] => Array ( [post_id] => [film_name] => ) [17] => Array ( [post_id] => [film_name] => ) [18] => Array ( [post_id] => [film_name] => ) [19] => Array ( [post_id] => [film_name] => ) [20] => Array ( [post_id] => [film_name] => ) [21] => Array ( [post_id] => [film_name] => ) [22] => Array ( [post_id] => [film_name] => ) [23] => Array ( [post_id] => [film_name] => ) [24] => Array ( [post_id] => [film_name] => ) [25] => Array ( [post_id] => [film_name] => ) [26] => Array ( [post_id] => [film_name] => ) [27] => Array ( [post_id] => [film_name] => ) [28] => Array ( [post_id] => [film_name] => ) [29] => Array ( [post_id] => [film_name] => ) [30] => Array ( [post_id] => [film_name] => ) [31] => Array ( [post_id] => [film_name] => ) [32] => Array ( [post_id] => [film_name] => ) [33] => Array ( [post_id] => [film_name] => ) [34] => Array ( [post_id] => [film_name] => ) [35] => Array ( [post_id] => [film_name] => ) [36] => Array ( [post_id] => [film_name] => ) [37] => Array ( [post_id] => [film_name] => ) [38] => Array ( [post_id] => [film_name] => ) [39] => Array ( [post_id] => [film_name] => ) [40] => Array ( [post_id] => [film_name] => ) [41] => Array ( [post_id] => [film_name] => ) [42] => Array ( [post_id] => [film_name] => ) [43] => Array ( [post_id] => [film_name] => ) [44] => Array ( [post_id] => [film_name] => ) [45] => Array ( [post_id] => [film_name] => ) [46] => Array ( [post_id] => [film_name] => ) ) Still empty.

    can you also print $film_names_array

    Thread Starter VierasTalo

    (@vierastalo)

    $film_names_array only gives out Array ( [0] => )

    if you see its not getting movie name and post id from array item with index 11

    I’m just not seeing why the arrays are not populating with values.

    That seems to be the issue here.

    If anything the $film_names_array should infact contain some data.

    Can you try adding print_r( $film_names_array ); directly after $film_names_array = preg_split( '/\r\n|\r|\n/', $film_names );.

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