[Plugin: Posts 2 Posts] How adapt a code to display several connected items?
-
I’m creating a website about tennis. I’ve got two custom post types: players and tournaments.
Each player has his own page and I would like to display (on this page) the name of the tournaments for which this certain player was finalist and the name of his corresponding opponent.
With the help of the plugin Posts 2 Posts, I created two 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' ) );
My knowledge of PHP is really basic but thanks to some help, I get this code which returns the loser of the final in case you are on the page of a player who won a tournament:
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; }
If the player won several tournaments, this code returns only the most recent finalist opponent.
From this my question is:
How can I adapt the code in order to get a list of the several opponents that the player has defeated at several finals of tournaments?Thanks in advance!
- The topic ‘[Plugin: Posts 2 Posts] How adapt a code to display several connected items?’ is closed to new replies.