As of WP Job Manager Field Editor version 1.6.3+ you can now use a filter to customize the order in which taxonomy values are output when using any of the available output methods. Below I will go over how the filter works, as well as provide an example to do so.
If you are not comfortable with adding filters/functions to your child theme’s functions.php file, I recommend installing the Code Snippets plugin and using that to add your custom code (I still use this a lot of the time myself).
Features
- Sorting is based on taxonomy Name and is case-insensitive (capitalization does not matter)
- Any values not included in the sort array will be added after the sorted values
Example Filter
The filter you would use to specify the custom sorting is based on the taxonomy you’re going to sort. If you don’t know the taxonomy slug, goto the screen where you can edit/add taxonomy values, and look at the URL in your browser, it is the value right after taxonomy= , as an example if this was my URL:
https://domain.com/wp-admin/edit-tags.php?taxonomy=food_styles&post_type=job_listing
The taxonomy slug would be food_styles
Using the example taxonomy above, the filter you would use, is as follows:
add_filter( 'field_editor_food_styles_tax_output_sort', 'custom_food_styles_sorting', 10, 4 );
As you can tell, the filter syntax would be:
field_editor_TAXONOMYSLUG_tax_output_sort
Example Function
In the example above, the function used for the filter is going to be custom_food_styles_sorting, with a priority of 10, and 4 args that will be passed to that function.
The 4 args that are passed to the function is as follows (you will probably not need all of them, but they are provided for reference or advanced developer usage):
$sort – first argument passed is an empty array array() , if you return an empty array nothing will be sorted
$meta_key – second argument is the meta key that is associated with this taxonomy and was called to be output
$listing_id – third argument is the listing ID that is being output
$args – fourth argument is an array of field configuration data
function custom_food_styles_sorting( $sort, $meta_key, $listing_id, $args ){ $my_sort = array( 'breakfast', 'Lunch', 'Dinner' ); return $my_sort; }
As you can see in the example above, in the function I have defined that I want the taxonomy output sorted in the order of ‘breakfast’, ‘Lunch’, ‘Dinner’
You will notice that I did not capitalize the first value breakfast , and that was on purpose to show you below, how sorting is done case-insensitive, to still sort correctly, even if there was a user input error (you did not capitalize correctly), or if capitalization is changed on the taxonomy itself, but not in the code for sorting.
Full Code Example
add_filter( 'field_editor_food_styles_tax_output_sort', 'custom_food_styles_sorting', 10, 4 ); function custom_food_styles_sorting( $sort, $meta_key, $listing_id, $args ){ $my_sort = array( 'breakfast', 'Lunch', 'Dinner' ); return $my_sort; }
Full Example Output:
Let’s say that on our example listing, the person who submitted the listing selected these taxonomy values:
Breakfast, Brunch, Lunch, Dinner
Normally without any custom sorting (without using the filter), the default output would be in alphabetical order, and would look like this:
Breakfast, Brunch, Dinner, Lunch
Notice how Lunch is showing after Dinner … which is not what we want, so to fix it, we add the example code from above to our site.
When we add our custom sorting code above to our child theme’s functions.php file (or using Code Snippets plugin), the order we defined was:
$my_sort = array( 'breakfast', 'Lunch', 'Dinner' );
The values output on the listing would then look like this:
Breakfast, Lunch, Dinner, Brunch
You will notice that the first value, Breakfast, the output is capitalized, but in our sort array we did not capitalize it. That is because the values you pass to the sorting filter are case-insensitive, the actual value (with capitalization) that will be output, is based on how you have it set in the taxonomy configuration (in WordPress admin area).
You may also notice that Brunch is now showing on the end. This is because you did not define Brunch in your sort array, and as such, it is added to the end of the sorted values when output. To include Brunch in the sorting, all you need to do is add Brunch to the array $my_sort = array( ‘breakfast’, ‘Brunch’, ‘Lunch’, ‘Dinner’ ); and then it will be included in the sorting and output like this:
Breakfast, Brunch, Lunch, Dinner
And voila! Profit!