• Resolved Sandra Romano

    (@sandraromano)


    Hi! I need to show the list of posts that result from a relationship in alphabetical order. Is there any way to achieve this? it is not an archive page, just a relationship pods field that appears in a single post. I am using custom templates, with each and magic tags. Thanks for any help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Paul Clark

    (@pdclark)

    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.www.ads-software.com/reference/classes/wp_query/

    Thread Starter Sandra Romano

    (@sandraromano)

    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!

    Plugin Support Paul Clark

    (@pdclark)

    Okay, great. Come back with how far you get or the template you’re trying to output if you have issues.

    Thread Starter Sandra Romano

    (@sandraromano)

    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!

    Thread Starter Sandra Romano

    (@sandraromano)

    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');
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Sort posts by title’ is closed to new replies.