Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Issue with Dynamic Data Term List Order

  • fortyfivecreative

    Hi, as per the screen grab on the link below, I’m using a query loop which includes ‘list terms’ for a ‘Destination’ taxonomy. Each post has a parent and child taxonomy term, and I’d like to always display the child THEN the parent. At the moment, it’s just outputting in alphabetical order, regardless of whether each term is a parent or child. Can you please help? Thanks! David

  • Hi there,

    You’ll need a custom solution for this.

    Here are a few steps:

    1. Under your Headline Block for the Destinations taxonomy, add another Headline Block.

    2. Add this class: cu-destinations Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    3. Add this snippet:

    // Create a custom sorting function to order terms by parent-child relationship
    function custom_term_sort($a, $b) {
        // Get term hierarchy level
        $level_a = count(get_ancestors($a->term_id, 'destinations'));
        $level_b = count(get_ancestors($b->term_id, 'destinations'));
    
        if ($level_a === $level_b) {
            return strcasecmp($a->name, $b->name); // Sort alphabetically when at the same level
        }
    
        return ($level_a > $level_b) ? -1 : 1; // Sort by hierarchy level
    }
    
    add_filter( 'render_block', function( $block_content, $block ) {
        if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'cu-destinations' ) !== false ) {
    		$current_post_id = get_the_ID();
    
    		// Get all "destinations" terms for the current post
    		$destinations = wp_get_post_terms($current_post_id, 'destinations');
    
    		// Sort the terms using the custom sorting function
    		usort($destinations, 'custom_term_sort');
    
    		// Loop through the sorted terms and display their names with links to term archive pages
    		if (!empty($destinations)) {
    			$block_content = '<div class="destionation-container">';
    			foreach ($destinations as $destination) {
    				$term_link = get_term_link($destination); // Get the link to the term archive
    				if (!is_wp_error($term_link)) {
    					$block_content .= '<a href="' . esc_url($term_link) . '">' . esc_html($destination->name) . '</a><br>'; // Display the term name with link
    				} 
    			}
    			$block_content .= "</div>";
    		}
        }
    
        return $block_content;
    }, 10, 2 );

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    Make sure to replace destinations in the code with your taxonomy slug.

    4. Delete the other Headline Block.

    Let us know how it goes.

  • fortyfivecreative

    Hi Fernando, Unfortunately this is still outputting in the same alphabetical order as before, rather than child then parent. The results mirror exactly the original headline blovk before this custom sort block. Thanks

  • fortyfivecreative

    Sorry! My fault, all working, thanks. I now just need to output the block as per the original block i.e. comma separated list with an icon to the left (as per the sccreenshot on the ticket). Are you able to help me adjust your custom function to achieve this? Thanks again!

  • Hi there,

    remove the code Fernando provided, and instead try this:

    function custom_get_the_terms_order($terms, $post_id, $taxonomy) {
        // check the taxonomy you want to apply it to
        if ($taxonomy === 'your_taxonomy') {
            $ordered_terms = array();
            $child_terms = array();
            $parent_terms = array();
    
            foreach ($terms as $term) {
                if ($term->parent === 0) {
                    $parent_terms[] = $term;
                } else {
                    $child_terms[] = $term;
                }
            }
    
            // Sort child terms alphabetically
            usort($child_terms, function ($a, $b) {
                return strcmp($a->name, $b->name);
            });
    
            $ordered_terms = array_merge($child_terms, $parent_terms);
    
            return $ordered_terms;
        }
    
        return $terms;
    }
    
    add_filter('get_the_terms', 'custom_get_the_terms_order', 10, 3);

    Note this line: if ($taxonomy === 'your_taxonomy') { change the your_taxonomy to what tax you want to apply it to.

  • fortyfivecreative

    Works perfectly, thank you!

  • Glad to hear that

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.