acf_get_valid_post_id()ACF 5.0.0

acf_get_valid_post_id

This function will return a valid post_id based on the current screen / parameter

Хуки из функции

Возвращает

$post_id. (mixed)

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

acf_get_valid_post_id( $post_id );
$post_id **
-

Список изменений

С версии 5.0.0 Введена.

Код acf_get_valid_post_id() ACF 6.0.4

function acf_get_valid_post_id( $post_id = 0 ) {

	// allow filter to short-circuit load_value logic
	$preload = apply_filters( 'acf/pre_load_post_id', null, $post_id );
	if ( $preload !== null ) {
		return $preload;
	}

	// vars
	$_post_id = $post_id;

	// if not $post_id, load queried object
	if ( ! $post_id ) {

		// try for global post (needed for setup_postdata)
		$post_id = (int) get_the_ID();

		// try for current screen
		if ( ! $post_id ) {

			$post_id = get_queried_object();

		}
	}

	// $post_id may be an object.
	// todo: Compare class types instead.
	if ( is_object( $post_id ) ) {

		// post
		if ( isset( $post_id->post_type, $post_id->ID ) ) {

			$post_id = $post_id->ID;

			// user
		} elseif ( isset( $post_id->roles, $post_id->ID ) ) {

			$post_id = 'user_' . $post_id->ID;

			// term
		} elseif ( isset( $post_id->taxonomy, $post_id->term_id ) ) {

			$post_id = 'term_' . $post_id->term_id;

			// comment
		} elseif ( isset( $post_id->comment_ID ) ) {

			$post_id = 'comment_' . $post_id->comment_ID;

			// default
		} else {

			$post_id = 0;

		}
	}

	// allow for option == options
	if ( $post_id === 'option' ) {

		$post_id = 'options';

	}

	// append language code
	if ( $post_id == 'options' ) {

		$dl = acf_get_setting( 'default_language' );
		$cl = acf_get_setting( 'current_language' );

		if ( $cl && $cl !== $dl ) {

			$post_id .= '_' . $cl;

		}
	}

	// filter for 3rd party
	$post_id = apply_filters( 'acf/validate_post_id', $post_id, $_post_id );

	// return
	return $post_id;

}