PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to change sorting in a list of product tags for WooCommerce products

 May 09, 2022     php, product, taxonomy-terms, woocommerce, wordpress     No comments   

Issue

I use this function to get and display all tag for each product :

public function get_product_tags_list() {
            global $product;

            if ( ! is_a( $product, 'WC_Product' ) ) {
                return;
            }

            $separator = '<span class="separator">&#44;&nbsp;</span></li><li>';
            $before    = '<ul><li>';
            $after     = '</li></ul>';

            return get_the_term_list( $product->get_id(), 'product_tag', $before, $separator, $after );
        }

how can I order the list of my tags by slug descending order ?

Regards


Solution

The only way to allow changing sorting options is to build your own function based on WordPress get_the_term_list() core function source code, replacing the function get_the_terms() by wp_get_post_terms() which allow additional parameters to alter the WP_Term_Query as follows:

public function get_product_tags_list() {
    global $product;

    if ( ! is_a( $product, 'WC_Product' ) ) {
        return;
    }

    $taxonomy  = 'product_tag'; // The taxonomy
    $args      = array( // Your sorting parameters below
        'orderby' => 'slug',
        'order'   => 'DESC',
    );

    $terms = wp_get_post_terms( $product->get_id(), $taxonomy, $args );
    
    if ( is_wp_error( $terms ) ) {
        return $terms;
    }
 
    if ( empty( $terms ) ) {
        return false;
    }
            
    $links = array();
 
    foreach ( $terms as $term ) {
        $link = get_term_link( $term, $taxonomy );
        
        if ( is_wp_error( $link ) ) {
            return $link;
        }
        $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
    }
    $term_links = apply_filters( "term_links-{$taxonomy}", $links );  // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
    
    $separator = '<span class="separator">&#44;&nbsp;</span></li><li>';
    $before    = '<ul><li>';
    $after     = '</li></ul>';
    
    return $before . join( $separator, $term_links ) . $after;
}

Code goes in functions.php file of the active child theme (or active theme). It should works.


Documentation:

  • WordPress get_the_term_list() function.
  • WordPress get_the_terms() function.
  • WordPress wp_get_post_terms() function.
  • WordPress WP_Term_Query parameters.


Answered By - LoicTheAztec
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing