Hướng dẫn sử dụng hàm get_terms (ok)

https://huyhoa.net/su-dung-get-terms-wordpress/

Trước tiên, get_terms nó là một hàm (function), còn WP_Term_Query nó là một lớp (class), nên tùy mục đích sử dụng mà chúng ta có thể sử dụng function hay là class. Bài viết này Huy Hòa chỉ hướng dẫn các bạn sử dụng hàm. Nếu có thời gian rảnh, mình sẽ viết hướng dẫn sử dụng lớp (class) sau. Tuy nhiên, với lớp WP_Term_Query thì phạm vi áp dụng nó mở rộng hơn và nó hoàn toàn có thể thay thế get_terms được.

Table of Contents [show]

Hàm get_terms() nằm ở đâu?

Hàm này nằm ở trong core nguồn của WordPress. Các bạn có thể tìm thấy hàm này tại file này: wp-includes/taxonomy.php. Đây là hàm nguyên bản của WordPress:

function get_terms( $args = array(), $deprecated = '' ) {
    $term_query = new WP_Term_Query();
 
    $defaults = array(
        'suppress_filter' => false,
    );
 
    /*
     * Legacy argument format ($taxonomy, $args) takes precedence.
     *
     * We detect legacy argument format by checking if
     * (a) a second non-empty parameter is passed, or
     * (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies)
     */
    $_args          = wp_parse_args( $args );
    $key_intersect  = array_intersect_key( $term_query->query_var_defaults, (array) $_args );
    $do_legacy_args = $deprecated || empty( $key_intersect );
 
    if ( $do_legacy_args ) {
        $taxonomies       = (array) $args;
        $args             = wp_parse_args( $deprecated, $defaults );
        $args['taxonomy'] = $taxonomies;
    } else {
        $args = wp_parse_args( $args, $defaults );
        if ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) {
            $args['taxonomy'] = (array) $args['taxonomy'];
        }
    }
 
    if ( ! empty( $args['taxonomy'] ) ) {
        foreach ( $args['taxonomy'] as $taxonomy ) {
            if ( ! taxonomy_exists( $taxonomy ) ) {
                return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
            }
        }
    }
 
    // Don't pass suppress_filter to WP_Term_Query.
    $suppress_filter = $args['suppress_filter'];
    unset( $args['suppress_filter'] );
 
    $terms = $term_query->query( $args );
 
    // Count queries are not filtered, for legacy reasons.
    if ( ! is_array( $terms ) ) {
        return $terms;
    }
 
    if ( $suppress_filter ) {
        return $terms;
    }
 
    /**
     * Filters the found terms.
     *
     * @since 2.3.0
     * @since 4.6.0 Added the `$term_query` parameter.
     *
     * @param array         $terms      Array of found terms.
     * @param array         $taxonomies An array of taxonomies.
     * @param array         $args       An array of get_terms() arguments.
     * @param WP_Term_Query $term_query The WP_Term_Query object.
     */
    return apply_filters( 'get_terms', $terms, $term_query->query_vars['taxonomy'], $term_query->query_vars, $term_query );
}

Đây cũng là hàm nằm trong core của WordPress nên ta chỉ việc sử dụng nó, còn phát triển và bảo mật là việc của …Wordpress lo.

Cách sử dụng hàm get_terms()

Kể từ phiên bản WordPress 4.5 cách sử dụng của hàm get_terms() có thay đổi chút.

Trong đó:

taxonomy: Giá trị truyền vào nhằm xác định tham số cần lấy thông tin.

hide_empty: Mặc định thì nó không lấy các “terms” mà không được gán với bài viết cụ thể nào. Nếu bạn vẫn muốn nó trả về những terms không gắn với bài viết nào thì bạn có thể chuyển cái false kia thành true là được

Các tham số có thể truyền vào của hàm get_terms là:

Khi sử dụng hàm này cho các custom field thì có thể sử dụng như ví dụ này:

Trong đó my_term là tên của taxonomy. Ví dụ như: Thể loại tour / Hạng tàu

get_terms() exclude

get_terms exclude by slug

get_terms parent – depth

get_terms depth – get only top-level terms in a custom taxonomy

Chọn parent thành 0 là nó chỉ lấy toàn bộ terms cha mẹ mà không hiện terms con như ví dụ bên trên ấy

List the child terms of a taxonomy and not their parents

Dựa vào cái parent này thì mình hoàn toàn có thể chỉ hiện nguyên các terms con, không hiện terms cha mẹ. Đây là ví dụ cụ thể:

Last updated

Was this helpful?