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 22.4

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 )
		->where_not_equal( 'post_status', 'trash' )
		->limit( 2 )    // Limit to 2 results to save time and resources.
		->find_array();

	// Get object_id from each subarray in $post_ids.
	$post_ids = ( is_array( $post_ids ) ) ? array_column( $post_ids, 'object_id' ) : [];

	/*
	 * 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;
}