Skip to content

Instantly share code, notes, and snippets.

@kadimi
Created April 15, 2023 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadimi/7b41c129cec3c1be80255e57b47daa42 to your computer and use it in GitHub Desktop.
Save kadimi/7b41c129cec3c1be80255e57b47daa42 to your computer and use it in GitHub Desktop.
Add links to tournaments on events
<?php
function sp_get_event_tournaments($event_id)
{
return get_posts([
'post_type' => 'sp_tournament',
'posts_per_page' => -1,
'meta_query' => [
[
'key' => 'sp_event',
'value' => $event_id,
]
]
]);
}
/**
* Add a player list on the fly to the current team.
*/
add_action(
'init',
function () {
add_filter(
'the_content',
function ($content) {
$additional_content = '';
$object = get_queried_object();
/**
* Exit if this is not a SportsPress event.
*/
if ('sp_event' !== $object->post_type) {
return $content;
}
$tournaments = sp_get_event_tournaments($object->ID);
if ($tournaments) {
ob_start();
?>
<h3>Tournaments</h3>
<ul>
<?php foreach ($tournaments as $tournament) : ?>
<li><a href="<?php the_permalink(); ?>"><?php echo get_the_title($tournament); ?></a></li>
<?php endforeach; ?>
</ul>
<?php
$additional_content = ob_get_clean();
}
/**
* Done
*/
return $content . $additional_content;
},
10
);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment