get_ancestors

Advertisement

Summery Summery

Get an array of ancestor IDs for a given object.

Syntax Syntax

get_ancestors( int $object_id, string $object_type = '', string $resource_type = '' )

Parameters Parameters

$object_id

(int) (Optional) The ID of the object. Default 0.

$object_type

(string) (Optional) The type of object for which we'll be retrieving ancestors. Accepts a post type or a taxonomy name.

Default value: ''

$resource_type

(string) (Optional) Type of resource $object_type is. Accepts 'post_type' or 'taxonomy'.

Default value: ''

Return Return

(int[]) An array of IDs of ancestors from lowest to highest in the hierarchy.

Source Source

File: wp-includes/taxonomy.php

	return $taxonomies;
}

/**
 * Retrieve all taxonomy names for the given post.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 * @return string[] An array of all taxonomy names for the given post.
 */
function get_post_taxonomies( $post = 0 ) {
	$post = get_post( $post );

	return get_object_taxonomies( $post );
}

/**
 * Determine if the given object is associated with any of the given terms.
 *
 * The given terms are checked against the object's terms' term_ids, names and slugs.
 * Terms given as integers will only be checked against the object's terms' term_ids.
 * If no terms are given, determines if object is associated with any terms in the given taxonomy.
 *
 * @since 2.7.0
 *
 * @param int              $object_id ID of the object (post ID, link ID, ...).
 * @param string           $taxonomy  Single taxonomy name.
 * @param int|string|array $terms     Optional. Term term_id, name, slug or array of said. Default null.
 * @return bool|WP_Error WP_Error on input error.
 */
function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
	$object_id = (int) $object_id;
	if ( ! $object_id ) {
		return new WP_Error( 'invalid_object', __( 'Invalid object ID.' ) );
	}

	$object_terms = get_object_term_cache( $object_id, $taxonomy );
	if ( false === $object_terms ) {
		$object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) );
		if ( is_wp_error( $object_terms ) ) {
			return $object_terms;

Advertisement

Changelog Changelog

Changelog
Version Description
4.1.0 Introduced the $resource_type argument.
3.1.0 Introduced.

Advertisement

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.