acf_field_gallery::ajax_get_sort_orderpublicACF 5.0.0

ajax_get_sort_order

description

Method of the class: acf_field_gallery{}

No Hooks.

Returns

$post_id. (int)

Usage

$acf_field_gallery = new acf_field_gallery();
$acf_field_gallery->ajax_get_sort_order();

Changelog

Since 5.0.0 Introduced.

acf_field_gallery::ajax_get_sort_order() code ACF 6.0.4

function ajax_get_sort_order() {

	// vars
	$r     = array();
	$order = 'DESC';
	$args  = acf_parse_args(
		$_POST, // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified below.
		array(
			'ids'       => 0,
			'sort'      => 'date',
			'field_key' => '',
			'nonce'     => '',
		)
	);

	// validate
	if ( ! wp_verify_nonce( $args['nonce'], 'acf_nonce' ) ) {

		wp_send_json_error();

	}

	// reverse
	if ( $args['sort'] == 'reverse' ) {

		$ids = array_reverse( $args['ids'] );

		wp_send_json_success( $ids );

	}

	if ( $args['sort'] == 'title' ) {

		$order = 'ASC';

	}

	// find attachments (DISTINCT POSTS)
	$ids = get_posts(
		array(
			'post_type'   => 'attachment',
			'numberposts' => -1,
			'post_status' => 'any',
			'post__in'    => $args['ids'],
			'order'       => $order,
			'orderby'     => $args['sort'],
			'fields'      => 'ids',
		)
	);

	// success
	if ( ! empty( $ids ) ) {

		wp_send_json_success( $ids );

	}

	// failure
	wp_send_json_error();

}