List a show’s Outlinks links and count their clicks from another add-on — a sponsor’s links, per episode and channel
Read Outlinks’ links and clicks through two functions, never by querying Outlinks’ own tables: the tables are not a published contract and may change. Sponsor Manager’s report uses them for a sponsor’s links.
benecaster_outlinks_get_redirects( ?int $show_id ) lists the show’s tracked links, oldest first, for a picker. Pass null for the site-wide links (those with no show; see Track a link that belongs to no show). Each is [ 'id', 'slug', 'label', 'destination_url', 'go_url' ]. label is the reference library item’s label, and null for a link whose item was deleted or has no label, and for every site-wide link. Store the id: it is what the counting function takes, and it survives a slug or destination edit.
benecaster_outlinks_count_clicks( ?int $show_id, ?array $redirect_ids, string $date_from, string $date_to ) counts human clicks (Outlinks’ bot filter applied) of the show’s links, or of the site-wide links for null, between two UTC dates, Y-m-d, both inclusive. It returns one row per link, episode and channel that has at least one click: [ 'redirect_id', 'episode_id', 'referrer_type', 'clicks' ], ordered by link, then episode (null first), then channel. Sum the rows for any total you need.
referrer_typeispage(a page of your own site, usually the episode page),rss(the link Outlinks wrote into the feed’s show notes, so a podcast app),email(the link Outlinks wrote into an episode email) orother(anywhere else, or a browser that sends no referrer). It is a reporting hint, not proof: a referrer can be missing or forged.episode_idis the episode the link was written into, counted only when that episode is the show’s own;nullwhen the link named none (always for a site-wide link).$redirect_idsnullcounts every link in scope. An empty array counts nothing, so a sponsor with no links assigned never gets the whole show’s clicks. An id that is not one of the show’s links counts nothing.
Code
<?php
// A sponsor's links on one show: clicks per episode and channel for November.
if ( function_exists( 'benecaster_outlinks_count_clicks' ) ) {
$rows = benecaster_outlinks_count_clicks( $show_id, $sponsor_redirect_ids, '2026-11-01', '2026-11-30' );
$by_episode = [];
foreach ( is_wp_error( $rows ) ? [] : $rows as $row ) {
$episode = $row['episode_id'] ?? 0; // 0: not from an episode.
$by_episode[ $episode ][ $row['referrer_type'] ] = ( $by_episode[ $episode ][ $row['referrer_type'] ] ?? 0 ) + $row['clicks'];
}
}
// The picker: every link of the show, by label or slug.
$choices = function_exists( 'benecaster_outlinks_get_redirects' ) ? benecaster_outlinks_get_redirects( $show_id ) : [];
Need this built rather than just documented? See our services →