Yoast\WP\SEO\Builders

Indexable_Hierarchy_Builder::find_primary_term_id_for_post()privateYoast 1.0

Gets the primary term ID for a post.

Метод класса: Indexable_Hierarchy_Builder{}

Хуков нет.

Возвращает

int. The primary term ID. 0 if none exists.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->find_primary_term_id_for_post( $post );
$post(WP_Post) (обязательный)
The post.

Код Indexable_Hierarchy_Builder::find_primary_term_id_for_post() Yoast 22.4

private function find_primary_term_id_for_post( $post ) {
	$main_taxonomy = $this->options->get( 'post_types-' . $post->post_type . '-maintax' );

	if ( ! $main_taxonomy || $main_taxonomy === '0' ) {
		return 0;
	}

	$primary_term_id = $this->get_primary_term_id( $post->ID, $main_taxonomy );

	if ( $primary_term_id ) {
		$term = \get_term( $primary_term_id );
		if ( $term !== null && ! \is_wp_error( $term ) ) {
			return $primary_term_id;
		}
	}

	$terms = \get_the_terms( $post->ID, $main_taxonomy );

	if ( ! \is_array( $terms ) || empty( $terms ) ) {
		return 0;
	}

	return $this->find_deepest_term_id( $terms );
}