Support

Account

Home Forums Front-end Issues Repeater values missing from query result when sorting by custom field.

Solving

Repeater values missing from query result when sorting by custom field.

  • Hi there! I’ve come across a really weird behaviour I don’t understand and which I need to solve somehow…

    I’m running ACF Pro Version 5.9.9

    Ok, I try to explain it as exact as possible:

    I do have a Query and inside the loop I display several custom fields, including the Cities from a repeater Group with one or more addresses. So inside the loop this is the basic code:

    
    $job_towns = get_field( "PostalAddress" );
    $job_level = get_field( "x-level" );
    
    [...]
    
    		<td class="jobTitle">
    			<?php the_title(); ?>
    		</td>
    		<td class="jobLevel"> 
    			<?php echo $job_level; ?>
    		</td>
    		<td class="jobCity"> 
    			<?php 
    				if ($job_towns) { 
    					foreach($job_towns as $city) {
    						echo '<p>' . $city['addressLocality'] . '</p>';
    					}
    				}
    			?>
    		</td>
    

    (Where the […] is I removed some html to keep it short).

    This works fine so far – the loop will correctly return a list of all the entries with their “levels” and the city/cities from the Adress-Repeater.

    Now here comes the problem: The whole output is supposed to be sortable, which does work fine if I sort it by the_title. But if I sort by a custom field like job_level then the cities are missing from the output. oO (the sorting itself seems to work fine as far as I can tell) .

    instead of

    “the_title” – “job_level” – “List of address localities”

    I get

    “the_title” – “job_level” – only an empty “<td></td>”

    So here’s what I have in my functions.php – there is a bunch of additional cases in my code (Sorting up or down based on other fields, but this is the gist of it):

    
    case "alphabetUp":
    					$query->set( 'orderby', 'title' );
    					$query->set( 'order', 'ASC' );
    					return $query;
    
    			case "levelDown":
    					$query->set( 'orderby', 'meta_value' );
    					$query->set( 'meta_key', 'x-level' );	
    					$query->set( 'order', 'DESC' );
    					return  $query;
    

    As I wrote before: The Ordering part just works just fine in both cases, so I know I didn’t make an error there, apparently. But when I want to Order the Query Output by ANY Custom field, then the City/Cities comes up as empty. Does anybody have an idea why? I’m at the end of my wits. ^^

    PS: I did already try alternative ways to get to the cities instead of a loop, for example:

    
    $job_towns = get_field( "PostalAddress" );
    $job_cities = array_column($job_towns, 'addressLocality');
    $job_cities = implode(", ", $job_cities);
    

    and then simply echo that string.

    But the result is the same. As soon as I sort based on a custom field the Cities are empty.

  • How are you looping over the posts returned by the query?

    To be honest, I don’t see any reason looking at the code you provided that the values would be shown other that then posts do not have any values set.

  • Hi there John!

    The loop is pretty much standard:

    
    if ( $posts ) {
        foreach ( $posts as $post ) :
            get_template_part( 'includes/loop-job', get_post_type());
        endforeach; 
        wp_reset_postdata();
    }
    

    Right? So at least I’m not missing something totally obvious. 😀 Thanks for that already! 🙂 I kinda hoped that there would be some kind of thing that everybody knew about except for me, but my code NOT being the obvious problem at least tells me I’m not a total idiot. 😉

    It might of course be some other plugin causing this problem.

    Now I could start and hunt down the culprit, but I just thought of a workaround that should do the trick as well, what do you think:

    The Problem is during sorting in a query and only hits the Address-Repeater section. I could of course hook into saving an article and THERE turn the cities into a string and save THAT in a normal (non-repeater)-field… Those aren’t affected by the glitch or whatever it is…

  • Yea, sorry, I’ve looked over everything again and I don’t see anything that seems wrong and I’m completely confused as to why it works when sorted by the title but does not when sorted by a field, it does not make any sense to me.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.