Wp_Helpers::select_custom_post_func()

Return all the specific fields of post type in ajax call


Source Source

File: src/integrations/wp/helpers/wp-helpers.php

	public function select_custom_post_func() {



		Automator()->utilities->ajax_auth_check( $_POST );
		$fields = array();
		if ( isset( $_POST ) && key_exists( 'value', $_POST ) && ! empty( $_POST['value'] ) ) {
			$post_type = sanitize_text_field( $_POST['value'] );

			$args = array(
				'posts_per_page'   => 999,
				'orderby'          => 'title',
				'order'            => 'ASC',
				'post_type'        => $post_type,
				'post_status'      => 'publish',
				'suppress_filters' => true,
				'fields'           => array( 'ids', 'titles' ),
			);

			$posts_list = Automator()->helpers->recipe->options->wp_query( $args );

			if ( ! empty( $posts_list ) ) {

				foreach ( $posts_list as $post_id => $title ) {

					$post_title = ! empty( $title ) ? $title : sprintf(
						/* translators: %1$s The ID of the post */
						esc_attr__( 'ID: %1$s (no title)', 'uncanny-automator' ),
						$post_id
					);

					$fields[] = array(
						'value' => $post_id,
						'text'  => $post_title,
					);
				}
			}
		}
		echo wp_json_encode( $fields );
		die();
	}