Skip Navigation

[Resolved] Sort a taxonomy by a custom field

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Minesh 5 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2701292

Hi in the search and pagination area of my view id like to sort a taxonomy drop down by a custom field. Im trying this where
artist-last-name is the slug uf the field id like to sort on. Should this be possable?

[wpv-control-post-taxonomy taxonomy="artist" type="select" default_label="Show All" orderby="artist-last-name" url_param="wpv-artist"]

Thanks

#2701392

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand you added a taxonomy as dropdown select frontend filter with the view and you have custom term field artist-last-name and you want to order the taxonomy terms by artist-last-name ASC - is that correct? If yes:

Toolset views offers the following hooks to filters the terms:
- wpv_filter_taxonomy_frontend_search_get_terms_args

Can you please check what hooks fits in best as per your requirement:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_frontend_search_get_terms_args

Using above hook, you should try to add the orderby param to order the terms by the custom term field: artist-last-name

For example:
You can add the following hook code to "Custom Code" section offered by Toolset:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

add_filter('wpv_filter_taxonomy_frontend_search_get_terms_args','func_order_taxonomy_filter_terms_by_custom_term_field', 10, 3 );
function func_order_taxonomy_filter_terms_by_custom_term_field( $args, $taxonomy, $view_id ) {
 
    if ( $view_id == 99999 && $taxonomy == 'artist' ){
		
		$args['meta_query'] = array('key' => 'wpcf-artist-last-name');
		$args['orderby'] = 'meta_value';
		$args['order'] = 'ASC';
		
	}
    return $args;
}


Where:
- Replace 99999 with your view ID
- Adjust the slugs for field or taxonomy where required.

Please check the following link that might help you:
- https://wordpress.stackexchange.com/questions/246975/order-get-terms-by-term-meta

#2701923

Thanks but im not able to get this to work
I get this error even though ive changed permissions to 755
Unfortunately, this snippet cannot be edited directly. Check file permissions and wp-config.php constants DISALLOW_FILE_EDIT and DISALLOW_FILE_MODS. Alternatively, you can edit the file manually.

However i creted the file manually and the codeappears in the snippet area. Ive adjusted the view id and the slugs and if i "save and test" it it runs without errors

Is there anything i can do here to trouble shoot?

Thanks

#2702019

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share details where you added the view with the taxonomy filter and send me admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2702113

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've adjusted the code added to the "Custom Code" section as given under:
- hidden link

add_filter('wpv_filter_taxonomy_frontend_search_get_terms_args','func_order_taxonomy_filter_terms_by_term_field', 10, 3 );
function func_order_taxonomy_filter_terms_by_term_field( $args, $taxonomy, $view_id ) {

    if ( $view_id == 44 && $taxonomy == 'artist' ){

		$args['meta_key'] = 'wpcf-artist-last-name';
		$args['orderby'] = 'meta_value';
		$args['order'] = 'ASC';

	}

    return $args;

}

Can you please confirm it works as expected now.

#2702243

Thanks Minesh Its fine now. Appreciate the help