Skip Navigation

[Resolved] Custom order on front end listing of custom taxonomies

This support ticket is created 3 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 11 replies, has 2 voices.

Last updated by LandruF8417 2 years, 12 months ago.

Assisted by: Shane.

Author
Posts
#2004527

I would like to reorder the listing of a custom taxonomy "industry" for a specific CPT.

I tried the codes from this support page, but could not get it to work. Not sure how to apply it if I did it wrong:

https://toolset.com/forums/topic/custom-taxonomies-admin-menu-order/

#2004537

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Landru,

Thank you for getting in touch.

Perhaps you can try this plugin below to help resolve the issue.
https://wordpress.org/plugins/taxonomy-terms-order/

Thanks,
Shane

#2004541

I saw the plugin option but I was hoping to find a solution where I do not need to add another plugin to the site.

The site already has a lot of plugins.

I was looking for a code I can use in the functions file or in the archive page.

#2005651

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Landru,

Is this on the frontend you are listing out the taxonomies correct? On a taxonomy archive ? Is this a custom archive created using our Blocks plugin ?

Please provide some further details so that I can know exactly what context you are working in so that I can best advise.

Thanks,
Shane

#2005913

It is on the front end that I am listing out the taxonomies. I am listing them out on the archive page of the CPT that the taxonomies belong to. I created the CPT with toolset Types. I created the archive page by creating the archive-**.php file for the archive page of the CPT. I'm building out the page using php, not the block plugin.

With my existing code, I am able to list out all the taxonomies but they are listed in alphabetical order. I just need to switch the the order of the first two items in the list.

Here is a portion of the code I'm using to display the listing of the taxonomies on the CPT's archive.php file :

<?php
						$terms = get_terms( 'industry', array(
							'orderby'   => 'menu_order title',
    						        'order'     => 'ASC',
							'hide_empty' => 0
						) );
						?>

foreach( $terms as $term ) {

...

} ?>

Thanks

#2006451

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Landru,

If this is a CPT archive it would be better to use this function below.
https://developer.wordpress.org/reference/functions/get_the_terms/

This will allow you to get the terms of each post being listed out in that CPT's archive. However the problem with this now is that it will return the term objects, so you will then need to build an array with just the term names and then use the php sorting function to sort it by alphabetical order.

If you're unsure of how to proceed on this, you can provide me with admin access and a link to the archive page so that I can assist you with this.

It should be noted that custom coding is out of the scope of our support forum, however I will see how best I can assist with this one.

Thanks,
Shane

#2006455

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Private Fields

#2006571

I can't share the login info because the site is not accessible online yet. But I am able to list out the terms (alphabetically). I just need to switch the position of the first two on the list.

The list of terms currently looks like this:

Collections
Financial
Legal
Private
Reposession

How can I get it to look like this:

Financial
Collections
Legal
Private
Reposession

#2006721

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Landru,

Thank you for the information.

You currently have your taxonomies sorted alphabetically, which is how you are getting it from the database.

Then you're building a $terms array with the foreach statement.

Given that this is the case you should have a regular array of where each value has its own index. So you can do this

$a = array(
    0 => 'Collections',
    1 => 'Financial',
    2 => 'Legal'
);
$temp = $a[0];
$a[0] = $a[1];
$a[1] = $temp;

Where $temp will temporarily hold the 0 index and then you will assign index 1 to index 0 and then reassign index 1 to the temp value.

At the end of this you should get

$a = array(
    0 => 'Financial',
    1 => 'Collections',
    2 => 'Legal'
);

Please let me know if this helps.

Thanks,
Shane

#2006805

I don't think I understand your solution. I could not get it to work. What do I have wrong in this code:


<ul >		
					<?php
						$termsb = get_terms( 'industry', array(
							'orderby'   => 'menu_order title',
    						       'order'     => 'ASC',
							'hide_empty' => 0
						) );
						?>
					
					<ul class="listing">	
					
					<?php
						foreach( $termsb as $termb ) {
 
					// Define the query
						$argsb = array(
						      //'post_type' => 'testimonials',
						      //'orderby'   => 'modified menu_order',
    						      //'order'     => 'ASC',
							'industry' => $termb->slug;
							 0 => 'Financial',
   							 1 => 'Collections',
  							 2 => 'Legal'							
						);
							$temp = $argsb[0];
							$argsb[0] = $argsb[1];
							$argsb[1] = $temp;
							
						$queryb = new WP_Query( $argsb );
							if( $queryb->have_posts() ) {
							echo '<li class="industry-' . $termb->slug . '">';
								echo $termb->slug ;	
							while ( $queryb->have_posts() ) : $queryb->the_post();
							$postid = get_the_ID();
																
					?>
						
					<?php endwhile;
							echo '</li>';
							}
						wp_reset_postdata();

					} ?>
					
</ul>

#2007555

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Landru,

My previous code was an example of how you can switch the values in the array. You were not to use this literal array.

<ul >     
                    <?php
                        $termsb = get_terms( 'industry', array(
                            'orderby'   => 'menu_order title',
                                   'order'     => 'ASC',
                            'hide_empty' => 0
                        ) );
                        ?>
                     $temp = $termsb[0];
  $termsb[0] = $termsb[1];
    $termsb[1] = $temp;
                    <ul class="listing">  
                     
                    <?php


                        foreach( $termsb as $termb ) {
  
                    // Define the query
                        $argsb = array(
                              'post_type' => 'testimonials',
                              'orderby'   => 'modified menu_order',
                                  'order'     => 'ASC',
                          
                        );
                            
                             
                        $queryb = new WP_Query( $argsb );
                            if( $queryb->have_posts() ) {
                            echo '<li class="industry-' . $termb->slug . '">';
                                echo $termb->slug ;  
                            while ( $queryb->have_posts() ) : $queryb->the_post();
                            $postid = get_the_ID();
                                                                 
                    ?>
                         
                    <?php endwhile;
                            echo '</li>';
                            }
                        wp_reset_postdata();
 
                    } ?>
                     
</ul>

Please test this code above and let me know if the desired results are being produced.
Thanks,
Shane

#2007635

Awesome!! Thanks Shane. That worked!

Now I understand the code you gave at first.

Thanks again!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.