get_terms example

$parsed_args = wp_parse_args( $args$defaults );

    $output = '';

    if ( ! is_array( $parsed_args['class'] ) ) {
        $parsed_args['class'] = explode( ' ', $parsed_args['class'] );
    }
    $parsed_args['class'] = array_map( 'sanitize_html_class', $parsed_args['class'] );
    $parsed_args['class'] = trim( implode( ' ', $parsed_args['class'] ) );

    if ( $parsed_args['categorize'] ) {
        $cats = get_terms(
            array(
                'taxonomy'     => 'link_category',
                'name__like'   => $parsed_args['category_name'],
                'include'      => $parsed_args['category'],
                'exclude'      => $parsed_args['exclude_category'],
                'orderby'      => $parsed_args['category_orderby'],
                'order'        => $parsed_args['category_order'],
                'hierarchical' => 0,
            )
        );
        if ( empty( $cats ) ) {
            

        $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'] );

        $total_terms = wp_count_terms( $count_args );

        // wp_count_terms() can return a falsey value when the term has no children.         if ( ! $total_terms ) {
            $total_terms = 0;
        }
echo wp_json_encode(
                        array(
                            'ID'         => $post->ID,
                            'post_title' => get_the_title( $post->ID ),
                            'post_type'  => $matches[2],
                        )
                    );
                    echo "\n";
                }
            }
        } elseif ( 'taxonomy' === $matches[1] ) {
            $terms = get_terms(
                array(
                    'taxonomy'   => $matches[2],
                    'name__like' => $query,
                    'number'     => 10,
                    'hide_empty' => false,
                )
            );

            if ( empty( $terms ) || is_wp_error( $terms ) ) {
                return;
            }

            
if ( ! current_user_can( $post_type_taxonomies[ $taxonomy ]->cap->assign_terms ) ) {
                        return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
                    }

                    /* * For hierarchical taxonomies, we can't assign a term when multiple terms * in the hierarchy share the same name. */
                    $ambiguous_terms = array();
                    if ( is_taxonomy_hierarchical( $taxonomy ) ) {
                        $tax_term_names = get_terms(
                            array(
                                'taxonomy'   => $taxonomy,
                                'fields'     => 'names',
                                'hide_empty' => false,
                            )
                        );

                        // Count the number of terms with the same name.                         $tax_term_names_count = array_count_values( $tax_term_names );

                        // Filter out non-ambiguous term names.
'3.0.0',
            sprintf(
                /* translators: 1: "type => link", 2: "taxonomy => link_category" */
                __( '%1$s is deprecated. Use %2$s instead.' ),
                '<code>type => link</code>',
                '<code>taxonomy => link_category</code>'
            )
        );
        $args['taxonomy'] = 'link_category';
    }

    $categories = get_terms( $args );

    if ( is_wp_error( $categories ) ) {
        $categories = array();
    } else {
        $categories = (array) $categories;
        foreach ( array_keys( $categories ) as $k ) {
            _make_cat_compat( $categories[ $k ] );
        }
    }

    return $categories;
}
$args['offset'] = ( $args['page'] - 1 ) * $args['number'];

        // Save the values because 'number' and 'offset' can be subsequently overridden.         $this->callback_args = $args;

        if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
            // We'll need the full set of terms then.             $args['number'] = 0;
            $args['offset'] = $args['number'];
        }

        $this->items = get_terms( $args );

        $this->set_pagination_args(
            array(
                'total_items' => wp_count_terms(
                    array(
                        'taxonomy' => $taxonomy,
                        'search'   => $search,
                    )
                ),
                'per_page'    => $tags_per_page,
            )
        );

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

    $clean_terms = array();
    foreach ( $terms as $term ) {
        // Empty terms are invalid input.         if ( empty( $term ) ) {
            continue;
        }

        $_term = get_terms(
            array(
                'taxonomy'   => $taxonomy,
                'name'       => $term,
                'fields'     => 'ids',
                'hide_empty' => false,
            )
        );

        if ( ! empty( $_term ) ) {
            $clean_terms[] = (int) $_term[0];
        } else {
            
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',
                'orderby'      => 'count',
                'order'        => 'DESC',
                'number'       => 10,
                'hierarchical' => false,
            )
        );
    }

    

    public function query( $query ) {
        $this->query_vars = wp_parse_args( $query );
        return $this->get_terms();
    }

    /** * Retrieves the query results. * * The return type varies depending on the value passed to `$args['fields']`. * * The following will result in an array of `WP_Term` objects being returned: * * - 'all' * - 'all_with_object_id' * * The following will result in a numeric string being returned: * * - 'count' * * The following will result in an array of text strings being returned: * * - 'id=>name' * - 'id=>slug' * - 'names' * - 'slugs' * * The following will result in an array of numeric strings being returned: * * - 'id=>parent' * * The following will result in an array of integers being returned: * * - 'ids' * - 'tt_ids' * * @since 4.6.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @return WP_Term[]|int[]|string[]|string Array of terms, or number of terms as numeric string * when 'count' is passed as a query var. */
/** * Filters the navigation menu objects being returned. * * @since 3.0.0 * * @see get_terms() * * @param WP_Term[] $menus An array of menu objects. * @param array $args An array of arguments used to retrieve menu objects. */
    return apply_filters( 'wp_get_nav_menus', get_terms( $args )$args );
}

/** * Determines whether a menu item is valid. * * @link https://core.trac.wordpress.org/ticket/13958 * * @since 3.2.0 * @access private * * @param object $item The menu item to check. * @return bool False if invalid, otherwise true. */
case 'name':
            $args['name'] = $value;
            break;
        case 'term_taxonomy_id':
            $args['term_taxonomy_id'] = $value;
            unset( $args['taxonomy'] );
            break;
        default:
            return false;
    }

    $terms = get_terms( $args );
    if ( is_wp_error( $terms ) || empty( $terms ) ) {
        return false;
    }

    $term = array_shift( $terms );

    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the DB.     if ( 'term_taxonomy_id' === $field ) {
        $taxonomy = $term->taxonomy;
    }

    
$tags  = array();
    $terms = array();
    if ( isset( $term ) && $term ) {
        $cat  = get_term( $term['term_id'], 'category' );
        $cats = array( $cat->term_id => $cat );
        unset( $term$cat );
    } elseif ( 'all' === $args['content'] ) {
        $categories = (array) get_categories( array( 'get' => 'all' ) );
        $tags       = (array) get_tags( array( 'get' => 'all' ) );

        $custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
        $custom_terms      = (array) get_terms(
            array(
                'taxonomy' => $custom_taxonomies,
                'get'      => 'all',
            )
        );

        // Put categories in order with no child going before its parent.         while ( $cat = array_shift( $categories ) ) {
            if ( ! $cat->parent || isset( $cats[ $cat->parent ] ) ) {
                $cats[ $cat->term_id ] = $cat;
            } else {
                
(array) $instance,
            array(
                'images'      => true,
                'name'        => true,
                'description' => false,
                'rating'      => false,
                'category'    => false,
                'orderby'     => 'name',
                'limit'       => -1,
            )
        );
        $link_cats = get_terms( array( 'taxonomy' => 'link_category' ) );
        $limit     = (int) $instance['limit'];
        if ( ! $limit ) {
            $limit = -1;
        }
        ?> <p> <label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Select Link Category:' ); ?></label> <select class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>"> <option value=""><?php _ex( 'All Links', 'links widget' ); ?></option> <?php foreach ( $link_cats as $link_cat ) : ?> <option value="<?php echo (int) $link_cat->term_id; ?>" <?php selected( $instance['category']$link_cat->term_id ); ?>>

    $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $taxonomy_object$search );

    /* * Require $term_search_min_chars chars for matching (default: 2) * ensure it's a non-negative, non-zero integer. */
    if ( ( 0 == $term_search_min_chars ) || ( strlen( $search ) < $term_search_min_chars ) ) {
        wp_die();
    }

    $results = get_terms(
        array(
            'taxonomy'   => $taxonomy,
            'name__like' => $search,
            'fields'     => 'names',
            'hide_empty' => false,
            'number'     => isset( $_GET['number'] ) ? (int) $_GET['number'] : 0,
        )
    );

    /** * Filters the Ajax term search results. * * @since 6.1.0 * * @param string[] $results Array of term names. * @param WP_Taxonomy $taxonomy_object The taxonomy object. * @param string $search The search term. */
$tab_index = $parsed_args['tab_index'];

    $tab_index_attribute = '';
    if ( (int) $tab_index > 0 ) {
        $tab_index_attribute = " tabindex=\"$tab_index\"";
    }

    // Avoid clashes with the 'name' param of get_terms().     $get_terms_args = $parsed_args;
    unset( $get_terms_args['name'] );
    $categories = get_terms( $get_terms_args );

    $name     = esc_attr( $parsed_args['name'] );
    $class    = esc_attr( $parsed_args['class'] );
    $id       = $parsed_args['id'] ? esc_attr( $parsed_args['id'] ) : $name;
    $required = $parsed_args['required'] ? 'required' : '';

    $aria_describedby_attribute = $parsed_args['aria_describedby'] ? ' aria-describedby="' . esc_attr( $parsed_args['aria_describedby'] ) . '"' : '';

    if ( ! $parsed_args['hide_if_empty'] || ! empty( $categories ) ) {
        $output = "<select $required name='$name' id='$id' class='$class'$tab_index_attribute$aria_describedby_attribute>\n";
    } else {
        
Home | Imprint | This part of the site doesn't use cookies.