a_z_listing_get_posts_by_title( string $post_title, string $post_type = '' )

Retrive posts by title.


Description


Parameters

$post_title

(string) (Required) the title to search for.

$post_type

(string) (Optional) the post type to search within.

Default value: ''


Return

(array<int,object>) the post IDs that are found.


Source

File: widgets/class-a-z-listing-widget.php

function a_z_listing_get_posts_by_title( string $post_title, string $post_type = '' ): array {
	global $wpdb;

	$params = array(
		's'                      => $post_title,
		'update_post_meta_cache' => false,
		'update_post_term_cache' => false,
	);
	if ( ! empty( $post_type ) ) {
		$params['post_type'] = $post_type;
	}

	add_filter( 'posts_search', 'a_z_listing_search_titles_only', 10, 2 );
	$query   = new WP_Query( $params );
	$results = $query->posts;
	remove_filter( 'posts_search', 'a_z_listing_search_titles_only' );

	return $results;
}

Changelog

Changelog
VersionDescription
4.0.0Use WP_Query
2.1.0Introduced.