• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / is_object_in_term() – Determine if the given object is associated with any of the given terms.

You appear to be a bot. Output may be restricted

Description

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.

Usage

$bool|WP_Error = is_object_in_term( $object_id, $taxonomy, $terms );

Parameters

$object_id
( int ) required – ID of the object (post ID, link ID, …).
$taxonomy
( string ) required – Single taxonomy name.
$terms
( int|string|array ) optional – Optional. Term term_id, name, slug or array of said. Default null.

Returns

bool|WP_Error WP_Error on input error.

Source

File name: wordpress/wp-includes/taxonomy.php
Lines:

1 to 59 of 59
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;
    }

    wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" );
  }

  if ( is_wp_error( $object_terms ) ) {
    return $object_terms;
  }
  if ( empty( $object_terms ) ) {
    return false;
  }
  if ( empty( $terms ) ) {
    return ( ! empty( $object_terms ) );
  }

  $terms = (array) $terms;

  $ints = array_filter( $terms, 'is_int' );
  if ( $ints ) {
    $strs = array_diff( $terms, $ints );
  } else {
    $strs =& $terms;
  }

  foreach ( $object_terms as $object_term ) {
    // If term is an int, check against term_ids only.
    if ( $ints && in_array( $object_term->term_id, $ints, true ) ) {
      return true;
    }

    if ( $strs ) {
      // Only check numeric strings against term_id, to avoid false matches due to type juggling.
      $numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) );
      if ( in_array( $object_term->term_id, $numeric_strs, true ) ) {
        return true;
      }

      if ( in_array( $object_term->name, $strs, true ) ) {
        return true;
      }
      if ( in_array( $object_term->slug, $strs, true ) ) {
        return true;
      }
    }
  }

  return false;
}
 

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 21st August 2020

Primary Sidebar

Information

Function name: is_object_in_term
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/taxonomy.php
File ref: wp-includes/taxonomy.php
Deprecated?: No
API Letters: I,O,T

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites