【備忘録】「前の記事」から特定のカテゴリを除外する方法

WordPress/Lightningの投稿ページのフッターに表示される「前の記事」「次の記事」部分で、特定のカテゴリを除外する時はget_previous_post()とかの前に以下を書いとけばできそう。
Lightningの場合はnext-prev.php(_g3/template-parts/next-prev.php)の中に書く感じ。

$excluded_terms = array(2, 13); // 除外したいカテゴリのIDを指定(複数指定可能)

$post_previous = get_previous_post(true, '', '', 'category');
$post_next     = get_next_post(true, '', '', 'category');
while ($post_previous && has_term($excluded_terms, 'category', $post_previous->ID)) {
    $post_previous = get_previous_post(true, '', '', 'category');
}
while ($post_next && has_term($excluded_terms, 'category', $post_next->ID)) {
    $post_next = get_next_post(true, '', '', 'category');
}

next-prev.phpの全体は以下。
ただスキム更新でリセットされるのでこの方法だと更新の都度変更が必要なので面倒。

<?php
global $bootstrap;
$in_same_term   = apply_filters( 'lightning_prev_next_post_in_same_term', false );
$excluded_terms = apply_filters( 'lightning_prev_next_post_excluded_terms', '' );
$taxonomy       = apply_filters( 'lightning_prev_next_post_taxonomy', 'category' );
?>

<?php

//ここから追記ーーーーーー
$excluded_terms = array(2, 13); // 除外したいカテゴリのIDを指定(複数指定可能)

$post_previous = get_previous_post(true, '', '', 'category');
$post_next     = get_next_post(true, '', '', 'category');
while ($post_previous && has_term($excluded_terms, 'category', $post_previous->ID)) {
    $post_previous = get_previous_post(true, '', '', 'category');
}
while ($post_next && has_term($excluded_terms, 'category', $post_next->ID)) {
    $post_next = get_next_post(true, '', '', 'category');
}
//ここまでーーーーーー
$post_previous = get_previous_post( $in_same_term, $excluded_terms, $taxonomy );
$post_next     = get_next_post( $in_same_term, $excluded_terms, $taxonomy );
if ( $post_previous || $post_next ) {
	$options = array(
		'layout'                     => 'card-intext',
		'display_image'              => true,
		'display_image_overlay_term' => false,
		'display_excerpt'            => false,
		'display_title'               => false,
		'display_date'               => true,
		'display_btn'                => false,
		'image_default_url'          => get_template_directory_uri() . '/assets/images/no-image.png',
		'overlay'                    => '',
		'body_prepend'               => '',
		'body_append'                => '',
	);
	?>

<div class="vk_posts next-prev">

	<?php
	if ( $post_previous ) {
		
		$options['overlay'] = '<span class="vk_post_imgOuter_singleTermLabel">' . __( 'Previous article', 'lightning' ) . '</span>';
		$options['class_outer']  = 'vk_post-col-xs-12 vk_post-col-sm-12 vk_post-col-md-6 next-prev-prev';
		$options                 = apply_filters( 'lightning_next_prev_options', $options );
		$options                 = apply_filters( 'lightning_next_prev_options_prev', $options );
		$post                    = $post_previous;
		/*
		Reason of $post = $post_previous;
		To use WordPress normal template-tag filter ( Like a 'post_thumbnail_html' filter )
			*/
		VK_Component_Posts::the_view( $post, $options );
		wp_reset_postdata();
	} else {
		echo '<div class="vk_post-col-xs-12 vk_post-col-sm-12 vk_post-col-md-6"></div>';
	} // if ( $post_previous ) {
	?>

	<?php
	if ( $post_next ) {
		$options['overlay'] = '<span class="vk_post_imgOuter_singleTermLabel">' . __( 'Next article', 'lightning' ) . '</span>';
		$options['class_outer']  = 'vk_post-col-xs-12 vk_post-col-sm-12 vk_post-col-md-6 next-prev-next';
		$options                 = apply_filters( 'lightning_next_prev_options', $options );
		$options                 = apply_filters( 'lightning_next_prev_options_next', $options );
		$post                    = $post_next;
		VK_Component_Posts::the_view( $post, $options );
		wp_reset_postdata();
	} else {
		echo '<div class="vk_post-col-xs-12 vk_post-col-sm-12 vk_post-col-md-6"></div>';
	} // if ( $post_next ) {
	?>

	</div>
	<?php
} // if ( $post_previous || $post_next ) {
?>