wp_get_object_terms example

if ( ! $taxonomy_obj->show_in_quick_edit ) {
                continue;
            }

            if ( isset( $tax_input[ $tax_name ] ) && current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
                $new_terms = $tax_input[ $tax_name ];
            } else {
                $new_terms = array();
            }

            if ( $taxonomy_obj->hierarchical ) {
                $current_terms = (array) wp_get_object_terms( $post_id$tax_name, array( 'fields' => 'ids' ) );
            } else {
                $current_terms = (array) wp_get_object_terms( $post_id$tax_name, array( 'fields' => 'names' ) );
            }

            $post_data['tax_input'][ $tax_name ] = array_merge( $current_terms$new_terms );
        }

        if ( isset( $new_cats ) && in_array( 'category', $tax_names, true ) ) {
            $cats                       = (array) wp_get_post_categories( $post_id );
            $post_data['post_category'] = array_unique( array_merge( $cats$new_cats ) );
            unset( $post_data['tax_input']['category'] );
        }

function get_the_terms( $post$taxonomy ) {
    $post = get_post( $post );

    if ( ! $post ) {
        return false;
    }

    $terms = get_object_term_cache( $post->ID, $taxonomy );

    if ( false === $terms ) {
        $terms = wp_get_object_terms( $post->ID, $taxonomy );
        if ( ! is_wp_error( $terms ) ) {
            $term_ids = wp_list_pluck( $terms, 'term_id' );
            wp_cache_add( $post->ID, $term_ids$taxonomy . '_relationships' );
        }
    }

    /** * Filters the list of terms attached to the given post. * * @since 3.1.0 * * @param WP_Term[]|WP_Error $terms Array of attached terms, or WP_Error on failure. * @param int $post_id Post ID. * @param string $taxonomy Name of the taxonomy. */
        if ( in_array( 'post', $fields, true ) ) {
            $_post = array_merge( $_post$post_fields );
        } else {
            $requested_fields = array_intersect_key( $post_fieldsarray_flip( $fields ) );
            $_post            = array_merge( $_post$requested_fields );
        }

        $all_taxonomy_fields = in_array( 'taxonomies', $fields, true );

        if ( $all_taxonomy_fields || in_array( 'terms', $fields, true ) ) {
            $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
            $terms                = wp_get_object_terms( $post['ID']$post_type_taxonomies );
            $_post['terms']       = array();
            foreach ( $terms as $term ) {
                $_post['terms'][] = $this->_prepare_term( $term );
            }
        }

        if ( in_array( 'custom_fields', $fields, true ) ) {
            $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
        }

        if ( in_array( 'enclosure', $fields, true ) ) {
            

    $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );

    if ( $in_same_term || ! empty( $excluded_terms ) ) {
        if ( $in_same_term ) {
            $join  .= " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
            $where .= $wpdb->prepare( 'AND tt.taxonomy = %s', $taxonomy );

            if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
                return '';
            }
            $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );

            // Remove any exclusions from the term array to include.             $term_array = array_diff( $term_array(array) $excluded_terms );
            $term_array = array_map( 'intval', $term_array );

            if ( ! $term_array || is_wp_error( $term_array ) ) {
                return '';
            }

            $where .= ' AND tt.term_id IN (' . implode( ',', $term_array ) . ')';
        }

        


        break;

    case 'move-down-menu-item':
        // Moving down a menu item is the same as moving up the next in order.         check_admin_referer( 'move-menu_item' );

        $menu_item_id = isset( $_REQUEST['menu-item'] ) ? (int) $_REQUEST['menu-item'] : 0;

        if ( is_nav_menu_item( $menu_item_id ) ) {
            $menus = isset( $_REQUEST['menu'] ) ? array( (int) $_REQUEST['menu'] ) : wp_get_object_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );

            if ( ! is_wp_error( $menus ) && ! empty( $menus[0] ) ) {
                $menu_id            = (int) $menus[0];
                $ordered_menu_items = wp_get_nav_menu_items( $menu_id );
                $menu_item_data     = (array) wp_setup_nav_menu_item( get_post( $menu_item_id ) );

                // Set up the data we need in one pass through the array of menu items.                 $dbids_to_orders = array();
                $orders_to_dbids = array();

                foreach ( (array) $ordered_menu_items as $ordered_menu_item_object ) {
                    
$args = array( 'taxonomy' => $taxonomy );

    $tax              = get_taxonomy( $taxonomy );
    $args['disabled'] = ! current_user_can( $tax->cap->assign_terms );

    $args['list_only'] = ! empty( $parsed_args['list_only'] );

    if ( is_array( $parsed_args['selected_cats'] ) ) {
        $args['selected_cats'] = array_map( 'intval', $parsed_args['selected_cats'] );
    } elseif ( $post_id ) {
        $args['selected_cats'] = wp_get_object_terms( $post_id$taxonomyarray_merge( $args, array( 'fields' => 'ids' ) ) );
    } else {
        $args['selected_cats'] = array();
    }

    if ( is_array( $parsed_args['popular_cats'] ) ) {
        $args['popular_cats'] = array_map( 'intval', $parsed_args['popular_cats'] );
    } else {
        $args['popular_cats'] = get_terms(
            array(
                'taxonomy'     => $taxonomy,
                'fields'       => 'ids',
                
if ( empty( $t['label'] ) ) {
            $t['label'] = $taxonomy;
        }

        if ( empty( $t['args'] ) ) {
            $t['args'] = array();
        }

        $terms = get_object_term_cache( $post->ID, $taxonomy );

        if ( false === $terms ) {
            $terms = wp_get_object_terms( $post->ID, $taxonomy$t['args'] );
        }

        $values = array();

        foreach ( $terms as $term ) {
            $values[] = $term->slug;
        }

        $t['value'] = implode( ', ', $values );

        $form_fields[ $taxonomy ] = $t;
    }

function get_terms_to_edit( $post_id$taxonomy = 'post_tag' ) {
    $post_id = (int) $post_id;
    if ( ! $post_id ) {
        return false;
    }

    $terms = get_object_term_cache( $post_id$taxonomy );
    if ( false === $terms ) {
        $terms = wp_get_object_terms( $post_id$taxonomy );
        wp_cache_add( $post_idwp_list_pluck( $terms, 'term_id' )$taxonomy . '_relationships' );
    }

    if ( ! $terms ) {
        return false;
    }
    if ( is_wp_error( $terms ) ) {
        return $terms;
    }
    $term_names = array();
    foreach ( $terms as $term ) {
        


/** * Retrieves the link category IDs associated with the link specified. * * @since 2.1.0 * * @param int $link_id Link ID to look up. * @return int[] The IDs of the requested link's categories. */
function wp_get_link_cats( $link_id = 0 ) {
    $cats = wp_get_object_terms( $link_id, 'link_category', array( 'fields' => 'ids' ) );
    return array_unique( $cats );
}

/** * Retrieves link data based on its ID. * * @since 2.0.0 * * @param int|stdClass $link Link ID or object to retrieve. * @return object Link object for editing. */

function wp_get_post_categories( $post_id = 0, $args = array() ) {
    $post_id = (int) $post_id;

    $defaults = array( 'fields' => 'ids' );
    $args     = wp_parse_args( $args$defaults );

    $cats = wp_get_object_terms( $post_id, 'category', $args );
    return $cats;
}

/** * Retrieves the tags for a post. * * There is only one default for this function, called 'fields' and by default * is set to 'all'. There are other defaults that can be overridden in * wp_get_object_terms(). * * @since 2.3.0 * * @param int $post_id Optional. The Post ID. Does not default to the ID of the * global $post. Default 0. * @param array $args Optional. Tag query parameters. Default empty array. * See WP_Term_Query::__construct() for supported arguments. * @return array|WP_Error Array of WP_Term objects on success or empty array if no tags were found. * WP_Error object if 'post_tag' taxonomy doesn't exist. */
$active_ancestor_item_ids    = array();
    $active_parent_item_ids      = array();
    $active_parent_object_ids    = array();
    $possible_taxonomy_ancestors = array();
    $possible_object_parents     = array();
    $home_page_id                = (int) get_option( 'page_for_posts' );

    if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
        foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
            if ( is_taxonomy_hierarchical( $taxonomy ) ) {
                $term_hierarchy = _get_term_hierarchy( $taxonomy );
                $terms          = wp_get_object_terms( $queried_object_id$taxonomy, array( 'fields' => 'ids' ) );
                if ( is_array( $terms ) ) {
                    $possible_object_parents = array_merge( $possible_object_parents$terms );
                    $term_to_ancestor        = array();
                    foreach ( (array) $term_hierarchy as $anc => $descs ) {
                        foreach ( (array) $descs as $desc ) {
                            $term_to_ancestor[ $desc ] = $anc;
                        }
                    }

                    foreach ( $terms as $desc ) {
                        do {
                            

function wp_delete_object_term_relationships( $object_id$taxonomies ) {
    $object_id = (int) $object_id;

    if ( ! is_array( $taxonomies ) ) {
        $taxonomies = array( $taxonomies );
    }

    foreach ( (array) $taxonomies as $taxonomy ) {
        $term_ids = wp_get_object_terms( $object_id$taxonomy, array( 'fields' => 'ids' ) );
        $term_ids = array_map( 'intval', $term_ids );
        wp_remove_object_terms( $object_id$term_ids$taxonomy );
    }
}

/** * Removes a term from the database. * * If the term is a parent of other terms, then the children will be updated to * that term's parent. * * Metadata associated with the term will be deleted. * * @since 2.3.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $term Term ID. * @param string $taxonomy Taxonomy name. * @param array|string $args { * Optional. Array of arguments to override the default term ID. Default empty array. * * @type int $default The term ID to make the default term. This will only override * the terms found if there is only one term found. Any other and * the found terms are used. * @type bool $force_default Optional. Whether to force the supplied term as default to be * assigned even if the object was not going to be term-less. * Default false. * } * @return bool|int|WP_Error True on success, false if term does not exist. Zero on attempted * deletion of default Category. WP_Error if the taxonomy does not exist. */

        $prepared_args = apply_filters( "rest_{$this->taxonomy}_query", $prepared_args$request );

        if ( ! empty( $prepared_args['post'] ) ) {
            $query_result = wp_get_object_terms( $prepared_args['post']$this->taxonomy, $prepared_args );

            // Used when calling wp_count_terms() below.             $prepared_args['object_ids'] = $prepared_args['post'];
        } else {
            $query_result = get_terms( $prepared_args );
        }

        $count_args = $prepared_args;

        unset( $count_args['number']$count_args['offset'] );

        

    function wxr_post_taxonomy() {
        $post = get_post();

        $taxonomies = get_object_taxonomies( $post->post_type );
        if ( empty( $taxonomies ) ) {
            return;
        }
        $terms = wp_get_object_terms( $post->ID, $taxonomies );

        foreach ( (array) $terms as $term ) {
            echo "\t\t<category domain=\"{$term->taxonomy}\" nicename=\"{$term->slug}\">" . wxr_cdata( $term->name ) . "</category>\n";
        }
    }

    /** * Determines whether to selectively skip post meta used for WXR exports. * * @since 3.3.0 * * @param bool $return_me Whether to skip the current post meta. Default false. * @param string $meta_key Meta key. * @return bool */
 elseif ( is_object( $bookmark ) ) {
        wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' );
        $_bookmark = $bookmark;
    } else {
        if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id == $bookmark ) ) {
            $_bookmark = & $GLOBALS['link'];
        } else {
            $_bookmark = wp_cache_get( $bookmark, 'bookmark' );
            if ( ! $_bookmark ) {
                $_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) );
                if ( $_bookmark ) {
                    $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) );
                    wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' );
                }
            }
        }
    }

    if ( ! $_bookmark ) {
        return $_bookmark;
    }

    $_bookmark = sanitize_bookmark( $_bookmark$filter );

    
Home | Imprint | This part of the site doesn't use cookies.