WPSEO_Meta::keyword_usage()public staticYoast 1.0

Counts the total of all the keywords being used for posts except the given one.

{} Это метод класса: WPSEO_Meta{}

Хуки из метода

Возвращает

Массив.

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

$result = WPSEO_Meta::keyword_usage( $keyword, $post_id );
$keyword(строка) (обязательный)
The keyword to be counted.
$post_id(int) (обязательный)
The id of the post to which the keyword belongs.

Код WPSEO_Meta::keyword_usage() Yoast 19.7.1

public static function keyword_usage( $keyword, $post_id ) {

	if ( empty( $keyword ) ) {
		return [];
	}

	/**
	 * The indexable repository.
	 *
	 * @var Indexable_Repository
	 */
	$repository = YoastSEO()->classes->get( Indexable_Repository::class );

	$post_ids = $repository->query()
		->select( 'object_id' )
		->where( 'primary_focus_keyword', $keyword )
		->where( 'object_type', 'post' )
		->where_not_equal( 'object_id', $post_id )
		->limit( 2 )
		->find_array();

	$callback = static function ( $row ) {
		return (int) $row['object_id'];
	};
	$post_ids = array_map( $callback, $post_ids );

	/*
	 * If Premium is installed, get the additional keywords as well.
	 * We only check for the additional keywords if we've not already found two.
	 * In that case there's no use for an additional query as we already know
	 * that the keyword has been used multiple times before.
	 */
	if ( count( $post_ids ) < 2 ) {
		/**
		 * Allows enhancing the array of posts' that share their focus keywords with the post's focus keywords.
		 *
		 * @param array  $post_ids The array of posts' ids that share their related keywords with the post.
		 * @param string $keyword  The keyword to search for.
		 * @param int    $post_id  The id of the post the keyword is associated to.
		 */
		$post_ids = apply_filters( 'wpseo_posts_for_focus_keyword', $post_ids, $keyword, $post_id );
	}

	return $post_ids;
}