Skip Navigation

[Resolved] Sorting an admin column for a custom field associated with a custom taxonomy

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to use Toolset custom fields in custom PHP codes?

Solution:

Types plugin will add prefix "wpcf-" before the field slug, so you need to use below field slug in your custom PHP codes, for example:

wpcf-taxonomy_term_order

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created 3 years, 7 months 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by Saul Baizman 3 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2209131

Hi there,

I have a custom field, Order (just a text input, into which someone can type a number like "5"), which is associated with a custom taxonomy, Curatorial Category.

The Order column appears on the taxonomy screen. It is not, however, interactive. I wish to click on it to sort the list of terms based on the content of the custom field.

I have followed the tutorial on this webpage:

hidden link

I've added the following code to my site:

// makes custom field sortable
function car_curatorial_category_sortable_columns( $columns ) {
    $columns['wpcf_field_homepage-display-order'] = 'wpcf_field_homepage-display-order';
	return $columns;
}
add_filter( 'manage_edit-curatorial-category_sortable_columns', 'car_curatorial_category_sortable_columns');

The tutorial above then says the next step is to modify the query using the pre_get_posts() hook. That didn't work for me, and I think it's because pre_get_posts() is for best posts (?).

I found another webpage with instructions specifically related to taxonomies at this address:

https://wordpress.stackexchange.com/questions/159330/custom-taxonomy-custom-sortable-column

Here's the code I tried (from the second response in the web address from the previous line):

function car_curatorial_categories_order_by( $term_query ) {
	global $pagenow;
	if(!is_admin()) {
		return $term_query;
	}

	if(is_admin() && $pagenow == 'edit-tags.php' && $term_query->query_vars['taxonomy'][0] == 'curatorial-category' && (!isset($_GET['orderby']) || $_GET['orderby'] == 'term_order')) {
		// set orderby to the named clause in the meta_query
		$term_query->query_vars['orderby'] = 'order_clause';
		$term_query->query_vars['order'] = isset($_GET['order']) ? $_GET['order'] : "DESC";
		// the OR relation and the NOT EXISTS clause allow for terms without a meta_value at all
		$args = array('relation' => 'OR',
		              'order_clause' => array(
			              'key' => 'taxonomy_term_order',
			              'type' => 'NUMERIC'
		              ),
			array(
				'key' => 'taxonomy_term_order',
				'compare' => 'NOT EXISTS'
			)
		);
		$term_query->meta_query = new WP_Meta_Query( $args );
	}
	return $term_query;
}
add_filter('pre_get_terms', 'car_curatorial_categories_order_by');

The code here used the pre_get_terms() hook, and this appears to be closer to the correct approach. But it still didn't quite work.

Can you offer some guidance?

Thank you.

Saul

#2209835

Hello,

I assume the custom taxonomy field "taxonomy_term_order" is created with Toolset Types plugin.

Types plugin will add prefix "wpcf-" before the field slug, so you need to use below slug "wpcf-taxonomy_term_order" in your custom PHP codes

More help:
https://toolset.com/documentation/customizing-sites-using-php/functions/

However, when you are accessing custom fields through native WordPress functions, you need to prepend the wpcf- prefix to the slug. Continuing from the above example, for native WordPress function to access the “House Price” field, you need to use the wpcf-house-price slug.

#2210281

Thanks, Luo! For anyone else trying to accomplish the same thing, here's the code:

1) Make the column heading sortable:

function car_curatorial_category_sortable_columns( $columns ) {
    // $columns['FIELD_NAME'] = 'YOUR-CUSTOM-FIELD-SLUG';
    // Note: "FIELD_NAME" came from the HTML markup and is *not* the same as the custom field slug.
    $columns['wpcf_field_homepage-display-order'] = 'wpcf-homepage-display-order';
	return $columns;
}
add_filter( 'manage_{SCREEN}_sortable_columns', 'car_curatorial_category_sortable_columns');

2) Create a WP_Meta_Query to add an "order by" clause to the SQL query:

function car_curatorial_categories_order_by( $term_query ) {
	global $pagenow;
	if(!is_admin()) {
		return $term_query;
	}
	// WP_Term_Query does not define a get() or a set() method so the query_vars member must
	// be manipulated directly
	if(is_admin() && $pagenow == 'edit-tags.php' && $term_query->query_vars['taxonomy'][0] == 'YOUR-CUSTOM-CATEGORY-SLUG' && ( isset($_GET['orderby']) && $_GET['orderby'] == 'YOUR-CUSTOM-FIELD-SLUG')) {
		// set orderby to the named clause in the meta_query
		$term_query->query_vars['orderby'] = 'order_clause';
		$term_query->query_vars['order'] = isset($_GET['order']) ? $_GET['order'] : 'desc';
		// the OR relation and the NOT EXISTS clause allow for terms without a meta_value at all
		$args = array(
                'relation' => 'OR',
                'order_clause' => array(
			        'key' => 'YOUR-CUSTOM-FIELD-SLUG',
			        'type' => 'NUMERIC'
                ),
			    array(
				    'key' => 'YOUR-CUSTOM-FIELD-SLUG',
				    'compare' => 'NOT EXISTS'
			    )
		);
		$term_query->meta_query = new WP_Meta_Query( $args );
	}
	return $term_query;
}
add_filter('pre_get_terms', 'car_curatorial_categories_order_by');