a_z_listing_autocomplete_post_titles()

Ajax responder for A_Z_Listing_Widget configuration


Description


Return

(void)


Source

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

function a_z_listing_autocomplete_post_titles() {
	check_ajax_referer( 'posts-by-title' );

	$nonce = '';
	if ( isset( $_REQUEST['_posts_by_title_wpnonce'] ) ) {
		$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_posts_by_title_wpnonce'] ) );
	}
	if ( ! wp_verify_nonce( $nonce, 'posts-by-title' ) ) {
		die( esc_html( __( 'Security check failed', 'a-z-listing' ) ) );
	}

	$post_title = '';
	$post_type  = '';
	if ( isset( $_POST['post_title']['term'] ) ) {
		$post_title = sanitize_text_field( wp_unslash( $_POST['post_title']['term'] ) );
	}
	if ( isset( $_POST['post_type'] ) ) {
		$post_type = sanitize_text_field( wp_unslash( $_POST['post_type'] ) );
	}

	$results = a_z_listing_get_posts_by_title( $post_title, $post_type );

	$titles = array();
	foreach ( $results as $result ) {
		$titles[] = array(
			'value' => intval( $result->ID ),
			'label' => addslashes( $result->post_title ),
		);
	}

	echo wp_json_encode( $titles );

	exit();
}

Changelog

Changelog
VersionDescription
2.0.0Introduced.