Support

Account

Home Forums Backend Issues (wp-admin) Can I use the text area to generate multiple meta values?

Solved

Can I use the text area to generate multiple meta values?

  • Hi, I’m under a… situation. I run a site with currently 455 articles about film. Aside from categories, we have no way of cataloging our texts for our readers. I am currently attempting to create a film-index. So, when a film is discussed in depth in an article, it’ll be given it’s own ACF metavalue. Then, on an alphabetized page (0-Z), using the selected first letter of the title when entering the value, the site pulls up all film titles with that first letter.

    The way it currently works is I have an initial ACF in which the writer picks which letters are used as the first letters of each film handled in said text, then this pops up a single row text box where they can input the full name of the film. This works perfectly, except for one thing:

    If there are more than one film starting with said letter in a text, there is no way to enter them in a way that it would look good when eventually searched in the alphabetized page. Now, I’ve noticed that the Text area -field lets you input multiple rows of text, however, those rows become a single block of “value” when put into it. Ergo, if we have a text with films Aa and Ac, then we have a separate text with film Ab, in the alphabetized page it will show up as
    Aa
    Ac
    Ab

    Whereas we would obviously want them to be in alphabetical order. I know that the repeater plug-in might fix this, as then we could just have multiple single row text boxes for each letter, however, it would be much easier if there was some way that we could change it so by using the text area -box each row of text would become its own value in the meta. We aren’t currently using ACF for anything else so I’d be open to poking at the source if that’s even possible.

    Thanks a lot for any assistance, and I apologize for any mistakes here; I’m hot garbage at coding and am amazed I’ve made it this far.

  • Hi @vierastalo

    I’m afraid I don’t understand the issue you have. Could you please share some screenshots of the issue and how you want it to be? If you can share the setup you have, that would be great too.

    Thanks!

  • Sure thing! So, we have a lot of articles. All of them about films. I want to compile a list of those films, ordered by the first letter of each title. Currently I have it set up as follows:

    Underneath the WYSIWYG-editor when writing an article, there’s an ACF for selecting the first letters of each film that appears in the article. When you select the letters, you get text areas into which you type, one row at a time, the names of the films in the text. Example:

    Then I use this data to create a page for each letter on the site that looks like this (the letters at the top link to the other pages but are non-functional for the moment until I lock this down):

    The code for this page looks 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 = get_posts(array(
    	'numberposts' => -1,
    	'meta_key' => 'elokuvat_k',
            'order'=> 'ASC',
            'orderby' => 'elokuvat_k'
    ));
    
    if($posts)
    {
    	echo '<ul>';
    
    	foreach($posts as $post)
    	{
    		echo '<li><a href="' . get_permalink($post->ID) . '">' . get_field($field_name='elokuvat_k', $post->ID) . '</a></li>';
    	}
    
    	echo '</ul>';
    }
    
    ?>
    
    	</div><!--/.pad-->
    	
    </section><!--/.content-->
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    This is all good. However, it becomes a problem if an article has two films in it that begin with the same letter. For example, if I add a film like this:

    Then it will show up like this on the page:

    The problem is that instead of creating two data entries, one of which would be Knock Knock (2015) and the other Kxick Test (2014), it creates a single entity which is known as
    Knock Knock (2015)
    Kxick Test (2014)

    This causes the alphabetical order of that page to become messy, as the lower of these two should be below, in this case, Kummeli V (2014), since it comes later in the alphabet.

    I wanted to know if there is any way to make it so that when entering multiple rows of text into the text area -box, the inputs would create their individual values into the database that the code takes them from, ergo instead of creating this single block of content it would create two separate ones.

  • Hi @vierastalo

    For something like this, you need to split the entries and then save it to a temporary variable like this:

    $temporary_list = array();
    foreach($posts as $post){
        $film_names = get_field('elokuvat_k', $post->ID);
        $film_names_array = preg_split( '/\r\n|\r|\n/', $string );
        foreach($film_names_array as $film_name){
            $temporary_list[] = array('post_id' => $post->ID, 'film_name' => $film_name);
        }
    }

    After that you need to sort the array using array_multisort() function like sorting a repeater.

    foreach( $temporary_list as $i => $row ) {
    	$order[ $i ] = $row['film_name'];
    }
    
    array_multisort( $order, SORT_ASC, $temporary_list );

    Then you can loop the $temporary_list variable to show the entries.

    I hope this makes sense.

  • Thanks for your hard work!

    Does all this code go into the page template? I attempted to get it to work by putting it there and editing the latter part appropriately but now the page doesn’t show anything. I made the code into 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
    
    $temporary_list = array();
    foreach($posts as $post){
        $film_names = get_field('elokuvat_k', $post->ID);
        $film_names_array = preg_split( '/\r\n|\r|\n/', $string );
        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, SORT_ASC, $temporary_list );
    
    if($posts)
    {
    	echo '<ul>';
    
    	foreach($posts as $post)
    	{
    		echo '<li><a href="' . get_permalink($post->ID) . '">' . get_field($temporary_list, $post->ID) . '</a></li>';
    	}
    
    	echo '</ul>';
    }
    
    ?>
    
    	</div><!--/.pad-->
    	
    </section><!--/.content-->
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Where did I go wrong?

  • Hi @vierastalo

    You need to get the field in the $post loop. Something 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 = get_posts(array(
    	'numberposts' => -1,
    	'meta_key' => 'elokuvat_k',
        'order'=> 'ASC',
        'orderby' => 'elokuvat_k'
    ));
    
    if($posts)
    {
    	echo '<ul>';
    
        $temporary_list = array();
    	foreach($posts as $post)
    	{
    		$film_names = get_field('elokuvat_k', $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, 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(); ?>

    If you don’t know how to modify it, please learn more about PHP here: http://www.w3schools.com/php/ or hire a developer to help you out with it. I recommend looking for one on https://studio.envato.com/ or https://www.upwork.com/.

    I hope this helps.

  • Thank you for the assistance. It totally now splits the entries into two separate ones, however it still doesn’t alphabetically sort them the proper way. I’ll try figuring this out on my own until our annual funding kicks in and then I’ll just get the repeater -add-on since that will certainly help with this matter. Thanks for all the help!

  • Hi @vierastalo

    I’m sorry about that.

    Could you please change this code:

    foreach( $temporary_list as $i => $row ) {
            $order[ $i ] = $row['film_name'];
        }
        array_multisort( $order, SORT_ASC, $temporary_list );

    to this one:

    $order = array();
    foreach( $temporary_list as $i => $row ) {
        $order[ $i ] = $row['film_name'];
    }
    array_multisort($order, SORT_ASC, SORT_STRING, $temporary_list );

    Hope this helps!

  • Hi, with some help from WP.org I managed to sort this out yesterday. For the record, the final code that we ended up using is the following:

    
    text/x-generic page-10787.php 
    PHP script text
    <?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_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>';
    }
    
    ?>
    
    	</div><!--/.pad-->
    
    </section><!--/.content-->
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.