Skip Navigation

[Closed] Sort archive page by last word of a custom field

This support ticket is created 4 years, 1 month 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
- 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 2 replies, has 2 voices.

Last updated by Minesh 4 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#2243795

Tell us what you are trying to do?
I'm trying to sort a category archive alphabetically by the last word of a custom field. I have a custom post type of research projects. Each project is categorized with a subcategory of the same parent category. The post type has a single field for a researcher's name, which is what I want to use for sorting. I have the code snippet below in an attempt to do this, but it's not working:

function sort_by_last_name($orderby, $query) {
// current_cat_is_sub_of is a custom function
$isResearchCategory = current_cat_is_sub_of(get_category_by_slug('research-cycles')->term_id);

if ($isResearchCategory && $query->is_main_query()) {
$orderby = "SUBSTRING_INDEX(wpcf-name-of-researcher, ' ', -1) ASC, wpcf-name-of-researcher ASC";
}
return $orderby;
}
add_filter('posts_orderby', 'sort_by_last_name',10,2);

Is there any documentation that you are following?
https://wordpress.stackexchange.com/questions/195034/taxonomy-terms-sort-by-last-name

#2244167

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL where you want to sort the post as well as admin access details so I can check whats going wrong with your setup.

Please note that this is custom code and we have limited support offered for such custom edits but I will still would like to give a hand if I can offer solution for your issue.

*** 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.

#2244997

Minesh
Supporter

Languages: English (English )

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

I see you are not even using the Toolset Blocks/Views to build your archive.

In that case - this is pure custom programming case and we offer limited support for such custom edits.

However, after seeing the problem URL you shared, I'm still not clear where I can find the research name custom field as well as what posts should be listed with the URL you shared? Can you please tell me what content you want to display on the problem URL you shared from what post type and where I can find the custom researcher name?

Also, can you please tell me where you added the custom code you shared with the initial post?

Once I review the information you shared, I will check if I can able to share any workaround otherwise you will have to contact our certified partners to deal with such custom programming case.

The topic ‘[Closed] Sort archive page by last word of a custom field’ is closed to new replies.