Skip to main content

Search a secondary data source and inject episode IDs into the core result set

Free Intermediate Since v1.0.0

Use when your add-on stores searchable content outside of wp_posts — transcripts, chapter titles, guest notes — and you want those episodes to appear in the standard [benecaster_search] results. Return the matching episode IDs; core merges them into the SQL so pagination totals stay correct. Apply your own visibility and tier gating before returning IDs — the filter result is trusted.

Code

<?php
add_filter(
    'benecaster_search_supplemental_episode_ids',
    function ( array $ids, string $term, int $show_id, array $tiers ): array {
        global $wpdb;

        $table = $wpdb->prefix . 'benecaster_transcripts';
        if ( ! $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) ) {
            return $ids;
        }

        $like = '%' . $wpdb->esc_like( $term ) . '%';

        // Exclude visibility='hidden' to prevent unpublished content leaking via search.
        $rows = $wpdb->get_col(
            $wpdb->prepare(
                "SELECT DISTINCT episode_id
                 FROM {$table}
                 WHERE show_id = %d
                   AND plain_text LIKE %s
                   AND visibility != 'hidden'",
                $show_id,
                $like
            )
        );

        return array_merge( $ids, array_map( 'intval', $rows ) );
    },
    10, 4
);

View on GitHub →

Hooks Used