Yoast\WP\SEO\Repositories

Indexable_Repository::find_posts_by_title_keywords │ public │ Yoast 1.0

Finds posts whose breadcrumb title contains any of the given comma-separated phrases.

The search string is a comma-separated list. Each value is matched as a whole contiguous substring of the breadcrumb title, and a post is returned when it contains any one of the values (a logical OR between values). For example, "hiking boots, trail" returns posts whose title contains "hiking boots" or "trail".

Results are paginated and ordered most recently modified first (with the indexable id as a stable tiebreaker), so requesting a later page returns older matches.

At most self::MAX_TITLE_KEYWORD_PHRASES phrases are honoured; any beyond that are ignored. The page size is clamped to the range 1..self::MAX_TITLE_KEYWORD_PAGE_SIZE.

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

Хуков нет.

Возвращает

Indexable[]. The matching indexables for the requested page, ordered by most recently modified.

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

$Indexable_Repository = new Indexable_Repository();
$Indexable_Repository->find_posts_by_title_keywords( $keywords, $page, $page_size, $post_type );
$keywords(строка) (обязательный)
The comma-separated phrases to match against the breadcrumb title.
$page(int)
The page of results to return, 1-based.
По умолчанию: 1
$page_size(int)
The number of posts per page.
По умолчанию: 10
$post_type(строка)
The post type to restrict the search to.
По умолчанию: 'post'

Код Indexable_Repository::find_posts_by_title_keywords() Yoast 28.7

public function find_posts_by_title_keywords( string $keywords, int $page = 1, int $page_size = 10, string $post_type = 'post' ) {
	$phrases = \array_map( 'trim', \explode( ',', $keywords ) );
	$phrases = \array_filter(
		$phrases,
		static function ( $phrase ) {
			return $phrase !== '';
		},
	);
	$phrases = \array_slice( $phrases, 0, self::MAX_TITLE_KEYWORD_PHRASES );

	// An empty search must not degrade into matching every post.
	if ( empty( $phrases ) ) {
		return [];
	}

	$likes  = \array_fill( 0, \count( $phrases ), 'breadcrumb_title LIKE %s' );
	$params = \array_map(
		function ( $phrase ) {
			return '%' . $this->wpdb->esc_like( $phrase ) . '%';
		},
		$phrases,
	);

	$page_size = \min( \max( 1, $page_size ), self::MAX_TITLE_KEYWORD_PAGE_SIZE );
	$offset    = ( ( \max( 1, $page ) - 1 ) * $page_size );

	$indexables = $this->query()
		->where( 'object_type', 'post' )
		->where( 'object_sub_type', $post_type )
		->where_raw( '( ' . \implode( ' OR ', $likes ) . ' )', \array_values( $params ) )
		->order_by_desc( 'object_last_modified' )
		->order_by_desc( 'id' )
		->limit( $page_size )
		->offset( $offset )
		->find_many();

	return \array_map( [ $this, 'upgrade_indexable' ], $indexables );
}