• 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 - 1 through 15 (of 42 total)
  • try this get all your movie names in an array. then use natcasesort()

    $movies = array('Le Movie 1', 'La Movie 1', 'La Movie2', 'Le Movie 2, 'La Moovie');
    natcasesort ($movies);
    foreach ($movies as $movie) {
        echo $movie,"<br>";
    }

    First it is recommended that you use the WP_Query class instead of the get_posts() function.

    Next, you’ll want to set your orderby parameter to ‘meta_value’, instead of the name of the meta_key.

    Example:

    $posts = new WP_Query( array(
    	'numberposts' => -1,
    	'meta_key' => 'elokuvat_c',
            'order'=> 'ASC',
            'orderby' => 'meta_value'
    ) );

    You can try setting the orderby parameter in your get_posts() call as well.

    $posts = get_posts( array(
    	'numberposts' => -1,
    	'meta_key' => 'elokuvat_c',
            'order'=> 'ASC',
            'orderby' => 'meta_value'
    ) );
    Thread Starter VierasTalo

    (@vierastalo)

    Hi Evan! Thanks for the assistance, however, then using the lower solution, there is no change to the order, and with the WP_Query I actually only get a single result instead of the many that usually come. It’s the first result in the regular query, so maybe it’s cutting something off for whatever reason? I’ve left it with the WP_Query at this page so you could check if you can tell what the problem might be: https://www.laajakuva.com/elokuvahakemisto/l/

    The code is currently like this:

    <?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 = new WP_Query( array(
    	'numberposts' => -1,
    	'meta_key' => 'elokuvat_l',
            'order'=> 'ASC',
            'orderby' => 'meta_value'
    ) );
    
    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 $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(); ?>

    bkdimri, I’m afraid I’m rather uncertain as to where the placement of the code you’ve mentioned should be.

    If you switched things over to WP_Query and didn’t re-write your loop, things won’t work properly. You need to re-code the entire loop.

    If using get_posts(), you should see more than one post being returned.

    I may not have a solid understanding of how your data is stored, so that may be the issue at hand.

    You might want to send over a screenshot of your admin interface, so we can get a better understanding of how the post data is structured.

    Thread Starter VierasTalo

    (@vierastalo)

    So, when using get_posts() and ordering by the meta_value, I get the same things as before using meta_value, in the same order. With WP_Query I get one. I did not however rewrite the loop, which is probably the issue. I tried using the WP_Query -wikipage for help, but only ended up with a lot of 500-errors.

    What part of the admin interface would you want?

    Currently, using ACF, there are fields under the editor when writing a post. First, you choose the letter that the film name starts with. This will pop up a window where you can type the film name. This area will generate the corresponding value (elokuvat_l for example for a film that begins with L). Here’s a picture of that: https://whatimg.com/viewer.php?file=uFijm9.jpg

    If you mean the WP admin interface, here it is. If there’s a specific area you’d want to hone in on, please just say: https://whatimg.com/i/ZRlUOF.jpg

    It’s hard to understand what’s going on because I only speak english ??

    In the first screenshot:
    https://whatimg.com/viewer.php?file=uFijm9.jpg

    Are each one of those containers a list of movies?

    For example, does the Elokuvat L text area contain all of the movies found on https://www.laajakuva.com/elokuvahakemisto/l/?

    I was under the impression that you had setup a custom post type, or were using standard posts, and each post was a standalone movie.

    You may also want to update your array_multisort function. The second parameter:

    array_multisort( $order, SORT_ASC, SORT_STRING );

    Thread Starter VierasTalo

    (@vierastalo)

    Oh, no, like I mentioned on the first post, I’m using a plug-in called Advanced Custom Fields ( https://www.advancedcustomfields.com/resources/ ) to create the fields that pop up underneath the post. The Elokuvat L -text area in that picture only contains the settings that make it pop up underneath the editor. The actual values are stored in the same way as Custom Fields usually are: https://codex.www.ads-software.com/Custom_Fields

    Editing the array_multisort seemed to have no effect.

    It’s hard to envision what is going on, as it’s not necessarily the way I would have gone about doing things.

    Maybe someone with a little more understanding of the back end of the site and how the movies are stored in the database can chime in with some additional tips.

    can you explain what function this line of code is doing?

    $film_names = get_field('elokuvat_l', $post->ID);
     $film_names_array = preg_split( '/\r\n|\r|\n/', $film_names );

    Thread Starter VierasTalo

    (@vierastalo)

    According to my understanding that’s the bit that’s doing the proverbial splitting of the entires. Eg, if I type into a box
    “The Godfather
    The Godfather II”

    And want them to appear on the site as separate entries, they need to be separated at the -point. Before applying said code the two appeared back to back, both as the same entity, on the list. Now they appear as two separate entities, but still back to back for whatever reason.

    Evan, thank you for putting up with my nonsense this far! I’m quite lost with most of this coding stuff so my way certainly might not be the best one.

    try this code

    <?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 = new WP_Query( array(
    	'numberposts' => -1,
    	'meta_key' => 'elokuvat_l',
            'order'=> 'ASC',
            'orderby' => 'meta_value'
    ) );
    
    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 $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 $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(); ?>

    The array_multisort function does not appear to be correct. Going off the documentation, there are 3 parameters that are accepted in that function:

    https://php.net/manual/en/function.array-multisort.php

    Parameter 1) The array being sorted
    Parameter 2) (optional) The sort order. Possible values ‘SORT_ASC’, ‘SORT_DESC’.
    Parameter 3) (optional) The sort flag. Possible values:
    ‘SORT_REGULAR’,
    ‘SORT_NUMERIC’,
    ‘SORT_STRING’,
    ‘SORT_LOCALE_STRING’,
    ‘SORT_NATURAL’,
    ‘SORT_FLAG_CASE’.

    so far what i have understood is the following
    1) Each movie is a post. You add movie by adding new post.
    2) You are using ACF to create some additional/custom fields which you call inside your create posts view
    3) There is a field for title created in ACF which you are using to get the title of movies
    4) you want to sort the list alphabetically

    is my understanding correct?

    Thread Starter VierasTalo

    (@vierastalo)

    bkdimri, I’ve applied the code you gave and here’s the result: https://www.laajakuva.com/elokuvahakemisto/l/

    So, there’s a plus-sign above the single result I was previously getting using WP_Query. Regarding your questions:

    1) Each post is an article about movies. It can contain writing on multiple movies.
    2) ACF creates the custom fields in the create posts view into which the writer enters the name of the film. Movies that begin with the same letter are input into the same text area, separate by <br>.
    3) This generates the field that I am using.
    4) Yes.

    Thank you for the assistance!

    EDIT: Evan, I tried replacing the last bit with SORT_STRING in the array in the code bkdimri provided but it didn’t change anything.

    I believe that bkdimri and I were attempting to solve a different problem. We were both under the impression that each one of your ‘movies’ was it’s own, standalone, post.

    At least I was under the impression that when you create a new movie, you go to ‘Posts > New’. Since each one of your movie titles is contained inside of a text area field – the query and sort functions I have been providing aren’t going to do anything.

    The problem is that the $temporary_list array is not being sorted properly. The query has no effect on what’s going on inside of that array.

    Right before the array_multisort() function, can you try printing out the contents of the $temporary_list array. Try adding:

    print_r( $temporary_list );

    before the array_multisort function, and let us know the structure of that array (copy paste what is printed on your page).

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