Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating a Tennis website. I've got 2 custom types : players and tournaments.

I would like to display the result of the finals on the tournaments page and players page. That is to say, on each page of tournament I want to display the name of the finalists (winner and loser) and on each page of player, I want to display the name of the tournaments for wich this certain player was finalist (and display his corresponding opponent). I hope I'm understandable!

With the help of the plugin Posts 2 Posts, I created 2 connections between the players and the tournaments:

    p2p_register_connection_type( array(
            'name' => 'won_final',
            'from' => 'player',
            'to' => 'tournament'
        ) );
and
    p2p_register_connection_type( array(
            'name' => 'lost_final',
            'from' => 'player',
            'to' => 'tournament'
        ) );

I have no idea of what code I can use to find and the display the corresponding opponent of a certain player when I display the tournaments for which the player was finalist.

Any help to put me on a track ? Thanks!

share|improve this question
1  
Please pay attention to code formatting. –  s_ha_dum Jan 9 '13 at 14:56

1 Answer 1

Maybe this could get you started:

function get_loser( $winner_id ) {
    global $post;
    $tournament = get_connected_id( $winner_id, 'won_final' );
    $loser  = get_connected_id( $tournament['ID'], 'lost_final' );

    return $loser;
}

function get_connected_id( $from_id, $connected_type ) {
    global $post;
    $result = array(
        'ID' => null,
        'post_title' => null,
    );

    if ( null === $from_id ) return $result;

    $connected = get_posts( array(
            'connected_type' => $connected_type,
            'connected_items' => $from_id,
            'nopaging' => true,
        ) 
    );
    if ( ! empty( $connected ) ) :
        $result['ID'] = $connected[0]->ID;
        $result['post_title'] = $connected[0]->post_title;
    endif;

    return $result;
}

Edit: If you want all the losing players from all the tournaments that a certain player won, then I suggest doing something like this:

function get_losers( $winner_id ) {
    global $post;

    // get all tournaments won
    $tournaments = get_connected_id( $winner_id, 'won_final' );

    // this is the resulting array, init it.
    $all_losers = array();

    // Go through all the tournaments and find the loser
    foreach( $tournaments as $tournament ):
        $losers  = get_connected_id( $tournament->ID, 'lost_final' );
        foreach( $losers as $loser ) :
            // Here's a new loser, store the id and tournament id
            $new_loser = array();
            $new_loser['ID'] = $loser->ID;
            // add more fields if you like
            $new_loser['tournament_id'] = $tournament->ID;
            $all_losers[] = $new_loser;
        endforeach;
    endforeach;

    // We are done return the array of losers.
    return $all_losers;
}

function get_connected_id( $from_id, $connected_type ) {
    global $post;
    $result = array(
        'ID' => null,
        'post_title' => null,
    );

    if ( null === $from_id ) return $result;

    $connected = get_posts( array(
            'connected_type' => $connected_type,
            'connected_items' => $from_id,
            'nopaging' => true,
        ) 
    );

    return $connected;
}
share|improve this answer
    
Thank you very much Windyjonas! You've met my expectations. In case the player won several tournaments, the function get_loser returns only the most recent opponent. I tried to add an while loop while ( $connected->have_posts() ) : $connected->the_post();) but I get an error from WP: "Call to a member function have_posts() on a non-object" –  Florianl Jan 9 '13 at 17:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.