get_term_field example

$error = new WP_Error();

        // Check if object id exists before saving.         if ( ! $prepared_nav_item['menu-item-object'] ) {
            // If taxonomy, check if term exists.             if ( 'taxonomy' === $prepared_nav_item['menu-item-type'] ) {
                $original = get_term( absint( $prepared_nav_item['menu-item-object-id'] ) );
                if ( empty( $original ) || is_wp_error( $original ) ) {
                    $error->add( 'rest_term_invalid_id', __( 'Invalid term ID.' ), array( 'status' => 400 ) );
                } else {
                    $prepared_nav_item['menu-item-object'] = get_term_field( 'taxonomy', $original );
                }
                // If post, check if post object exists.             } elseif ( 'post_type' === $prepared_nav_item['menu-item-type'] ) {
                $original = get_post( absint( $prepared_nav_item['menu-item-object-id'] ) );
                if ( empty( $original ) ) {
                    $error->add( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 400 ) );
                } else {
                    $prepared_nav_item['menu-item-object'] = get_post_type( $original );
                }
            }
        }

        
 else {
        /* * If non-custom menu item, then: * - use the original object's URL. * - blank default title to sync with the original object's title. */

        $args['menu-item-url'] = '';

        $original_title = '';
        if ( 'taxonomy' === $args['menu-item-type'] ) {
            $original_parent = get_term_field( 'parent', $args['menu-item-object-id']$args['menu-item-object'], 'raw' );
            $original_title  = get_term_field( 'name', $args['menu-item-object-id']$args['menu-item-object'], 'raw' );
        } elseif ( 'post_type' === $args['menu-item-type'] ) {

            $original_object = get_post( $args['menu-item-object-id'] );
            $original_parent = (int) $original_object->post_parent;
            $original_title  = $original_object->post_title;
        } elseif ( 'post_type_archive' === $args['menu-item-type'] ) {
            $original_object = get_post_type_object( $args['menu-item-object'] );
            if ( $original_object ) {
                $original_title = $original_object->labels->archives;
            }
        }
$original_object = get_post( $item->object_id );
            if ( $original_object ) {
                /** This filter is documented in wp-includes/post-template.php */
                $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );

                if ( '' === $original_title ) {
                    /* translators: %d: ID of a post. */
                    $original_title = sprintf( __( '#%d (no title)' )$original_object->ID );
                }
            }
        } elseif ( 'taxonomy' === $item->type && ! empty( $item->object_id ) ) {
            $original_term_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
            if ( ! is_wp_error( $original_term_title ) ) {
                $original_title = $original_term_title;
            }
        } elseif ( 'post_type_archive' === $item->type ) {
            $original_object = get_post_type_object( $item->object );
            if ( $original_object ) {
                $original_title = $original_object->labels->archives;
            }
        }
        $original_title = html_entity_decode( $original_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
        return $original_title;
    }

function term_description( $term = 0, $deprecated = null ) {
    if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) {
        $term = get_queried_object();
        if ( $term ) {
            $term = $term->term_id;
        }
    }

    $description = get_term_field( 'description', $term );

    return is_wp_error( $description ) ? '' : $description;
}

/** * Retrieves the terms of the taxonomy that are attached to the post. * * @since 2.5.0 * * @param int|WP_Post $post Post ID or object. * @param string $taxonomy Taxonomy name. * @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms * or the post does not exist, WP_Error on failure. */
Home | Imprint | This part of the site doesn't use cookies.