Relationships are stored in meta as an array of Post IDs, so there’s no way to sort them with [each]. It would need to be a PHP WP_Query or pods() query. The easiest method would likely be WP_Query with orderby and order attributes, passing the Post IDs from relationship meta as post__in.
See https://developer.ww.wp.xz.cn/reference/classes/wp_query/
Oh, thank you very much! I am going to try that, then. I am not php-savvy at all, let’s see what happens. Thanks a lot!
Okay, great. Come back with how far you get or the template you’re trying to output if you have issues.
Thanks!
I have tried to make a shortcode, to be able to put all this in an Elementor page. This is what I have so far, but it doesn’t work:
function terminos_relacionados_alphabetic( $atts ) {
$referencia = pods('referencia', get_the_ID() );
$args = [
'name' => 'termino_contenido',
'orderby' => 'title',
'order' => DESC,
];
$pod = $referencia->field( $args );
if( $pod->exists() ){
$related = $pod->field('termino_contenido.ID');
echo '<ul class="hoja">';
if ( ! empty( $related ) ) {
foreach ( $related as $id ) {
$title = get_the_title($id);
$link = get_permalink($id);
echo '<li><a href="' . $link . '">' . $title . '</a></li>';
}
echo '</ul>';
}
}
}
add_shortcode( 'term_rel', 'terminos_relacionados_alphabetic');
Any idea? Many thanks!
Nevermind! I’ve done it. Here’s the code:
function terminos_contenidos_enorden( $atts ) {
$pod = pods( 'referencia', get_the_id() );
$params = array(
'orderby' => 'post_title'
);
$related = $pod->field( 'termino_contenido', $params );
if ( ! empty( $related ) ) {
echo '<ul class="hoja">';
foreach ( $related as $rel ) {
$id = $rel[ 'ID' ];
echo '<li><a href="'.esc_url( get_permalink( $id ) ).'">'.get_the_title( $id ).'</a></li>';
}
echo '</ul>';
} //endif ! empty ( $related )
}
add_shortcode( 'term_con', 'terminos_contenidos_enorden');