• Resolved jeffmcnear

    (@jeffmcnear)


    I have two custom post types that share a bi-directional relationship field. On one where there can be several relationships (“organizations” may have multiple “opportunities”) I can display the associated field just fine using a snippet found here: https://pods.io/tutorials/get-values-from-a-custom-relationship-field/
    The other content type “opportunity” can have only one related field for “organization”. I am trying to show a link to the organization using this modified snippet:
    <?php
    //get Pods object for current post
    $pod = pods( ‘opportunity’, get_the_id() );
    //get the value for the relationship field
    $related = $pod->field( ‘organization’ );
    //loop through related field, creating links to their own pages
    //only if there is anything to loop through
    if ( ! empty( $related ) ) {
    echo ‘‘.’Organization:’;

    //show the related post name as link
    echo
    ‘.get_the_title( $id ).’‘ ;
    //get the value for some_field in related post and echo it

    } //endif ! empty ( $related )
    ?>
    Unfortunately the link that shows is a link back to the current “opportunity” …. any ideas what I am doing wrong? — thanks in advance, and I think PODS is terrific!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter jeffmcnear

    (@jeffmcnear)

    This works (should have read through things more carefully)

    The key line is: $id = $related[ ‘ID’ ];

    <?php
    //get Pods object for current post
    $pod = pods( ‘opportunity’, get_the_id() );
    //get the value for the relationship field
    $related = $pod->field( ‘organization’ );
    //loop through related field, creating links to their own pages
    //only if there is anything to loop through
    if ( ! empty( $related ) ) {

    //get id for related post and put in ID
    //for advanced content types use $id = $rel[ ‘id’ ];
    $id = $related[ ‘ID’ ];
    //show the related post name as link
    echo’‘. ‘Organization: ‘.’‘. ‘‘.get_the_title( $id ).’‘;

    } //endif ! empty ( $related )
    ?>

    Plugin Contributor Jim True

    (@jimtrue)

    Glad you figured it out, marking resolved.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Showing link to post with bi-directional relationship’ is closed to new replies.