Skip to main content

benecaster_get_sole_show_id()

Utilities Free
benecaster_get_sole_show_id(): ?int

Returns the show ID on a single-show install, and nothing on a multi-show one. “Active” here means published and not archived.

This is what backs the optional show_id attribute on every Benecaster shortcode: omit show_id on a one-show site and it resolves to that show; on a multi-show site the caller has to say which one. Use it for the same purpose in your own shortcodes and blocks, so they behave the way the built-in ones do.

Return Value

Type: int|null

The post ID of the single active show, or null when there are zero active shows or two or more of them. Both cases answer null.

Example

// The show_id-optional pattern every Benecaster shortcode uses.
$show_id = ! empty( $atts['show_id'] )
    ? (int) $atts['show_id']
    : benecaster_get_sole_show_id();

if ( ! $show_id ) {
    return '<p>' . esc_html__( 'Please pass a show_id — this site has more than one show.', 'my-addon' ) . '</p>';
}

Notes

null is not an error, and it does not mean "no shows". Zero active shows and five active shows both answer null — the question it really answers is "can a show be inferred without being told?". The correct response to null is to require an explicit show_id, never to fall back to a guess such as the most recent show.

Safe to call before the plugin container has booted. It instantiates ShowRepository directly rather than resolving it, and turns any throwable into null, so it is usable from early hooks where a container lookup would fatal.

Need this built rather than just documented? See our services →