• Resolved becca37

    (@becca37)


    (the password for the staging site is $t@g!ng) (Ignore the quality of the image. Ignore the weird icons.) (staging site will be gone after deploy to production ; if you’re here after that happens you won’t see the staging site live))

    I have a custom post type created in Pods, called Dogs. within that I have a bi-directional relationship to another custom post type, called Distinctions.

    I’m able to display the Dog, grab the related Distinctions. Yay.

    Now, how do I SORT the Distinctions by a particular field? In my case I want to sort by it’s name so distinctions.Distinction-name.

    Here’s how I’m parsing the data.

    
    $distinctions = $dogs->field( 'distinctions' ); // would like to get this SORTED								
    if (! empty ($distinctions) )
    {
    	foreach ( $distinctions as $distEarned ) 
    	{ 
    		$distID = $distEarned[ 'ID' ];
    		$distName = get_post_meta( $distID, 'distinction-name', true );
    		$distDesc = get_post_meta( $distID, 'description', true );
    		$distDesc = str_replace('<p>', '', $distDesc);
    		$distDesc = str_replace('</p>', '', $distDesc);
    
    		$useClass = 'dog-dist-' . str_replace(' ', '-', strtolower($distName));
    		$useText = $distName;
    		if ($distName === 'Champion')
    		{
    			$useText = 'Conformation Champion';
    		}				
    
    		$thisDistinctionIcon = '<span class="' . $useClass . '" alt="' . $useText . '" title="' . $distDesc .'"></span>';
    		$distinctionsEarnedText = $distinctionsEarnedText . '<tr><td>' . $thisDistinctionIcon . '</td><td>' . $useText . '</td><td>' . $distDesc . '</td></tr>';
    		$distinctionsEarnedIcons = $distinctionsEarnedIcons . $thisDistinctionIcon;
    	}
    }
    
    • This topic was modified 3 years, 7 months ago by becca37.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @becca37

    Relationship fields are sorted in the backend manually.
    If you need to create a custom sort on the frontend then maybe this will help: https://stackoverflow.com/a/2699159/2539261

    Cheers, Jory

    Thread Starter becca37

    (@becca37)

    Thanks, that does the trick. :0)

    Code snip adjusted from above …

    
    ...
    if (! empty ($distinctions) )
    {
    	array_multisort(array_map(function($element) 
    	{
    		return $element['post_title'];
    	}, $distinctions), SORT_ASC, $distinctions);				
    
    	foreach ( $distinctions as $distEarned ) 
    	{ 
    ...
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Relationship – sort order’ is closed to new replies.