wp_set_object_terms example

if ( isset( $post['errors'] ) ) {
                $errors[ $attachment_id ] = $post['errors'];
                unset( $post['errors'] );
            }

            if ( $post != $_post ) {
                wp_update_post( $post );
            }

            foreach ( get_attachment_taxonomies( $post ) as $t ) {
                if ( isset( $attachment[ $t ] ) ) {
                    wp_set_object_terms( $attachment_idarray_map( 'trim', preg_split( '/,+/', $attachment[ $t ] ) )$t, false );
                }
            }
        }
    }

    if ( isset( $_POST['insert-gallery'] ) || isset( $_POST['update-gallery'] ) ) {
        ?> <script type="text/javascript"> var win = window.dialogArguments || opener || parent || top; win.tb_remove(); </script>
$post = apply_filters( 'attachment_fields_to_save', $post$attachment_data );

    if ( isset( $post['errors'] ) ) {
        $errors = $post['errors']; // @todo return me and display me!         unset( $post['errors'] );
    }

    wp_update_post( $post );

    foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {
        if ( isset( $attachment_data[ $taxonomy ] ) ) {
            wp_set_object_terms( $idarray_map( 'trim', preg_split( '/,+/', $attachment_data[ $taxonomy ] ) )$taxonomy, false );
        }
    }

    $attachment = wp_prepare_attachment_for_js( $id );

    if ( ! $attachment ) {
        wp_send_json_error();
    }

    wp_send_json_success( $attachment );
}

$terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
    }

    /* * Hierarchical taxonomies must always pass IDs rather than names so that * children with the same names but different parents aren't confused. */
    if ( is_taxonomy_hierarchical( $taxonomy ) ) {
        $terms = array_unique( array_map( 'intval', $terms ) );
    }

    return wp_set_object_terms( $post_id$terms$taxonomy$append );
}

/** * Sets categories for a post. * * If no categories are provided, the default category is used. * * @since 2.1.0 * * @param int $post_id Optional. The Post ID. Does not default to the ID * of the global $post. Default 0. * @param int[]|int $post_categories Optional. List of category IDs, or the ID of a single category. * Default empty array. * @param bool $append If true, don't delete existing categories, just add on. * If false, replace the categories with the new categories. * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. */

    protected function handle_terms( $post_id$request ) {
        $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );

        foreach ( $taxonomies as $taxonomy ) {
            $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;

            if ( ! isset( $request[ $base ] ) ) {
                continue;
            }

            $result = wp_set_object_terms( $post_id$request[ $base ]$taxonomy->name );

            if ( is_wp_error( $result ) ) {
                return $result;
            }
        }
    }

    /** * Checks whether current user can assign all terms sent with the current request. * * @since 4.7.0 * * @param WP_REST_Request $request The request object with post and terms data. * @return bool Whether the current user can assign the provided terms. */

        do_action( 'wp_add_nav_menu_item', $menu_id$menu_item_db_id$args );
    }

    /* * Associate the menu item with the menu term. * Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms(). */
    if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
        $update_terms = wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
        if ( is_wp_error( $update_terms ) ) {
            return $update_terms;
        }
    }

    if ( 'custom' === $args['menu-item-type'] ) {
        $args['menu-item-object-id'] = $menu_item_db_id;
        $args['menu-item-object']    = 'custom';
    }

    $menu_item_db_id = (int) $menu_item_db_id;

    
if ( 1 === count( $terms ) && isset( $default ) ) {
            $terms = array( $default );
        } else {
            $terms = array_diff( $terms, array( $term ) );
            if ( isset( $default ) && isset( $force_default ) && $force_default ) {
                $terms = array_merge( $terms, array( $default ) );
            }
        }

        $terms = array_map( 'intval', $terms );
        wp_set_object_terms( $object_id$terms$taxonomy );
    }

    // Clean the relationship caches for all object types using this term.     $tax_object = get_taxonomy( $taxonomy );
    foreach ( $tax_object->object_type as $object_type ) {
        clean_object_term_cache( $object_ids$object_type );
    }

    $term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) );
    foreach ( $term_meta_ids as $mid ) {
        delete_metadata_by_mid( 'term', $mid );
    }

function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
    // If $link_categories isn't already an array, make it one:     if ( ! is_array( $link_categories ) || 0 === count( $link_categories ) ) {
        $link_categories = array( get_option( 'default_link_category' ) );
    }

    $link_categories = array_map( 'intval', $link_categories );
    $link_categories = array_unique( $link_categories );

    wp_set_object_terms( $link_id$link_categories, 'link_category' );

    clean_bookmark_cache( $link_id );
}

/** * Updates a link in the database. * * @since 2.0.0 * * @param array $linkdata Link data to update. See wp_insert_link() for accepted arguments. * @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success. */
Home | Imprint | This part of the site doesn't use cookies.