Home › Toolset Professional Support › [Resolved] Order after numbers which are separated with spaces
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/Karachi (GMT+05:00)
We have a page which shows all estates sales this year.
The custom fields are single line. This is because from the database they are already separated with spaces.
So the first question is how these custom fields should be transform into numbers that can be ordered by numeric values. I guess there must be made a function or something, but I can`t find any similar topics on your support forum. Right now it starts with 990 000, and goes down to 500 000, followed by 4 900 000. And so it goes. Any ideas?
The second question is that from the database the prices are imported from 3 "categories", or what I should name them. These are "Sold for", "transfered for" and "share tranfered for". I am not sure about the translation, but that does not really matter. All that matter is that these are separate custom fields. My question is therefor if there are any possibilities to order all these within one button, e.g. calling it "price". Meaning that when someone press to order it with price, both "sold for", "transfered for" and "share tranfered for" are taken into count and ordered by the value. Is this possible?
Hi Tom,
Thank you for contacting us and I'll be happy to assist.
Question 1:
If you have a custom field of "single line" type, that includes numbers and spaces, it can be used for numerical comparisons for sorting or filtering.
While selecting a field for ordering/sorting, you'll see an option to set the comparison to "As a number".
( ref: https://toolset.com/documentation/user-guides/allowing-visitors-to-sort-the-front-end-results/ )
Screenshot showing ordering options:
hidden link
Screenshot showing sorting options:
hidden link
Question 2:
To add sortable filter for the sum of your 3 prices from 3 different custom fields, you can first create a new custom field for storing the sum of those prices and then use that field for the sorting/ordering.
To automatically update the value (count of prices) of this new custom field, whenever a post is saved, you can use a function similar to:
function auto_sum_prices( $post_ID ) { if ( get_post_type( $post_ID ) == 'post-type-slug' ) { $price_1 = get_post_meta($post_ID, 'wpcf-price-1', true); $price_2 = get_post_meta($post_ID, 'wpcf-price-2', true); $price_2 = get_post_meta($post_ID, 'wpcf-price-3', true); $num_price_1 = str_replace(' ', '', $price_1); $num_price_2 = str_replace(' ', '', $price_2); $num_price_3 = str_replace(' ', '', $price_3); $count = $num_price_1 + $num_price_2 + $num_price_3; update_post_meta( $post_ID, 'wpcf-sum-or-records', $count ); } } add_action( 'save_post', 'auto_sum_prices', 99 );
You'll need to update the slugs of custom post type and custom fields, with the actual slugs used on your website.
For further assistance with custom code, you can consult one of our certified consultants from this link:
hidden link
You will get the custom assistance you need to get on with your project.
Note: For future reference, we’ll recommend to include one question per ticket for the most efficient and timely resolution, as mentioned in our support policy:
https://toolset.com/toolset-support-policy/
I hope this helps! Please let us know if you need any further assistance.
regards,
Waqar
Hello Waqar,
Thank you for helping me out.
Question 1:
Great, I didn`t see the option "as a number" at first. However, it still does not order correctly. Please see screenshot. It looks correctly, but then suddently all the million numbers are appearing below 100 000. How do I solve this problem?
Question 2:
Great idea! I did not think about that. The code has been added and replaced with slugs. However, it does not work. Is there something I have missed? The custom field "wpcf-sum" does not look like it`s getting updated.
function auto_sum_prices( $post_ID ) { if ( get_post_type( $post_ID ) == 'post-type-eiendom' ) { $price_1 = get_post_meta($post_ID, 'wpcf-solgt-for', true); $price_2 = get_post_meta($post_ID, 'wpcf-overdratt-for', true); $price_2 = get_post_meta($post_ID, 'wpcf-andel-overdratt-for', true); $num_price_1 = str_replace(' ', '', $price_1); $num_price_2 = str_replace(' ', '', $price_2); $num_price_3 = str_replace(' ', '', $price_3); $count = $num_price_1 + $num_price_2 + $num_price_3; update_post_meta( $post_ID, 'wpcf-sum', $count ); } } add_action( 'save_post', 'auto_sum_prices', 99 );
Hi Tom,
Thanks for writing back and for sharing the screenshot.
Question 1:
If the ordering is not working properly with existing custom field values, it would be best to strip the spaces from them.
In case you'll need the price in that same format to show on the frontend, you can alternatively create a new shadow custom field and store price value without empty spaces. You'll be able to use that numeric shadow custom field price for the ordering comparison.
To automatically update the value of a custom field, without spaces, the code will be very similar to the one shared last time.
( ref: https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/ )
Question 2:
In your screenshot, the custom post type's slug is shown as "eiendom". Please replace it with "post-type-eiendom", that you used in your code.
Operations for Questions 1 and 2 can be performed through a single function as:
function auto_process_prices( $post_ID ) { if ( get_post_type( $post_ID ) == 'eiendom' ) { // get original price values with spaces $price_1 = get_post_meta($post_ID, 'wpcf-solgt-for', true); $price_2 = get_post_meta($post_ID, 'wpcf-overdratt-for', true); $price_3 = get_post_meta($post_ID, 'wpcf-andel-overdratt-for', true); // remove empty spaces from the prices $num_price_1 = str_replace(' ', '', $price_1); $num_price_2 = str_replace(' ', '', $price_2); $num_price_3 = str_replace(' ', '', $price_3); // update new values without spaces into price fields update_post_meta( $post_id, 'wpcf-solgt-for', $num_price_1, $price_1 ); update_post_meta( $post_id, 'wpcf-overdratt-for', $num_price_2, $price_2 ); update_post_meta( $post_id, 'wpcf-andel-overdratt-for', $num_price_3, $price_3 ); // get sum of all prices $count = $num_price_1 + $num_price_2 + $num_price_3; // update the sum in the Sum custom field update_post_meta( $post_ID, 'wpcf-sum', $count ); } } add_action( 'save_post', 'auto_process_prices', 99 );
Important Note: The above function won't process all price field values in the database, at once. It will only trigger for an individual "eiendom" custom post type item, when "Save" or "Update" button will be clicked on its edit screen, in the admin area.
If you have a lot of posts already entered and manually updating each one is not an option, you can consult one of our certified consultants, to get a more complex code that can automate the process further.
hidden link
Please let me know if there's anything else I can help you with.
Have a great weekend!
Thank you,
Everything seems logic, but except the part that it does not work. The sum does not get updated, and the spaces between the numbers are not being removed.
Any ideas?
Here is what I am using to make sure I am using the correctly:
function auto_process_prices( $post_ID ) { if ( get_post_type( $post_ID ) == 'post-type-eiendom' ) { // get original price values with spaces $price_1 = get_post_meta($post_ID, 'wpcf-solgt-for', true); $price_2 = get_post_meta($post_ID, 'wpcf-overdratt-for', true); $price_3 = get_post_meta($post_ID, 'wpcf-andel-overdratt-for', true); // remove empty spaces from the prices $num_price_1 = str_replace(' ', '', $price_1); $num_price_2 = str_replace(' ', '', $price_2); $num_price_3 = str_replace(' ', '', $price_3); // update new values without spaces into price fields update_post_meta( $post_id, 'wpcf-solgt-for', $num_price_1, $price_1 ); update_post_meta( $post_id, 'wpcf-overdratt-for', $num_price_2, $price_2 ); update_post_meta( $post_id, 'wpcf-andel-overdratt-for', $num_price_3, $price_3 ); // get sum of all prices $count = $num_price_1 + $num_price_2 + $num_price_3; // update the sum in the Sum custom field update_post_meta( $post_ID, 'wpcf-sum', $count ); } } add_action( 'save_post', 'auto_process_prices', 99 );
Can you please if I am missing something?
Hi Tom,
In the code you've shared, I still see the post type slug as:
if ( get_post_type( $post_ID ) == 'post-type-eiendom' ) {
and not as:
if ( get_post_type( $post_ID ) == 'eiendom' ) {
Can you please update that and then test it again by saving/updating any "eiendom" post type from the admin area?
Hello again,
I thought you ment the opposite. Because when testet I only got an error. Please see below
Warning: A non-numeric value encountered in /customers/6/2/7/hitra24.no/httpd.www/wp-content/themes/hitratjuefire/functions.php on line 283 Warning: A non-numeric value encountered in /customers/6/2/7/hitra24.no/httpd.www/wp-content/themes/hitratjuefire/functions.php on line 283 Warning: Cannot modify header information - headers already sent by (output started at /customers/6/2/7/hitra24.no/httpd.www/wp-content/themes/hitratjuefire/functions.php:283) in /customers/6/2/7/hitra24.no/httpd.www/wp-includes/pluggable.php on line 1219
Line 283 is the following:
$count = $num_price_1 + $num_price_2 + $num_price_3;
Hi Tom,
This warning suggests that those price custom fields may include some non-numeric characters.
Here is the updated code which checks for numeric values and won't execute if any of the involved prices will have any characters other than numbers (1-9) or empty space:
function auto_process_prices( $post_id, $post ) { if ( get_post_type( $post_id ) == 'eiendom' ) { // get original price values with spaces $price_1 = get_post_meta($post_id, 'wpcf-solgt-for', true); $price_2 = get_post_meta($post_id, 'wpcf-overdratt-for', true); $price_3 = get_post_meta($post_id, 'wpcf-andel-overdratt-for', true); // remove empty spaces from the prices $num_price_1 = str_replace(' ', '', $price_1); $num_price_2 = str_replace(' ', '', $price_2); $num_price_3 = str_replace(' ', '', $price_2); // only process if all price values are numeric if (is_numeric($num_price_1) && is_numeric($num_price_2) && is_numeric($num_price_3)) { // update new values without spaces into price fields update_post_meta( $post_id, 'wpcf-solgt-for', $num_price_1, $price_1 ); update_post_meta( $post_id, 'wpcf-overdratt-for', $num_price_2, $price_2 ); update_post_meta( $post_id, 'wpcf-andel-overdratt-for', $num_price_3, $price_3 ); // get sum of all prices $count = $num_price_1 + $num_price_2 + $num_price_3; // update the sum in the Sum custom field update_post_meta( $post_id, 'wpcf-sum', $count ); } } } add_action( 'save_post', 'auto_process_prices', 99 );
Please make sure that the code is added as it is and that your price custom fields don't include characters, other than numbers and spaces.
Note: in case the filtering and automatic custom field processing, still doesn't work, I'll suggest hiring a professional who can analyze the existing price data first and then add custom programming accordingly.
We have a list of certified consultants at:
https://toolset.com/contractors/
Thank you Waqar,
I am sorry to inform you that it resultet in a fatal error:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function auto_process_prices(), 1 passed in /customers/6/2/7/hitra24.no/httpd.www/wp-includes/class-wp-hook.php on line 288 and exactly 2 expected in /customers/6/2/7/hitra24.no/httpd.www/wp-content/themes/hitratjuefire/functions.php:263 Stack trace: #0 /customers/6/2/7/hitra24.no/httpd.www/wp-includes/class-wp-hook.php(288): auto_process_prices(12750) #1 /customers/6/2/7/hitra24.no/httpd.www/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array) #2 /customers/6/2/7/hitra24.no/httpd.www/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #3 /customers/6/2/7/hitra24.no/httpd.www/wp-includes/post.php(3673): do_action('save_post', 12750, Object(WP_Post), true) #4 /customers/6/2/7/hitra24.no/httpd.www/wp-includes/post.php(3746): wp_insert_post(Array, false) #5 /customers/6/2/7/hitra24.no/httpd.www/wp-admin/includes/post.php(377): wp_update_post(Array) #6 /customers/6/2/7/hitra24.no/httpd.www/wp-admin/post.php(194): edit_post() #7 {main} throw in /customers/6/2/7/hitra24.no/httpd.www/wp-content/themes/hitratjuefire/functions.php on line 263
Still something you can help me with, or should I leave it?
Image shows how the custom field was set.
The error also appeared when I removed the white space and added a number for the other fields.
Edit: Even though I get the error, the page gets updated. However, this error came up no matter which post type which was updated/saved/published.
Hi Tom,
I apologize for a typo and please remove the "$post" from the first line.
Replace it from:
function auto_process_prices( $post_id, $post ) {
To:
function auto_process_prices( $post_id ) {
This will fix this fatal error, from all post/page edit screens.
Thank you, that solved the fatal error.
But I am sorry to inform that this function does not work. Not quite sure if the posts are being updated. I have "updated" about 10 posts and these are some of them. The first one is 700 000, followed by 2 450 000, and at last 1 300 000.
Any ideas? Or should I give up?
Hi Tom,
Can you please confirm if you've clicked the "Update" button, on each of these post's edit screen?
(after adding the updated code)
As explained before, the code executes only for a single post, when it is saved or updated from its edit screen. It works as expected on my test website.
Since 1-1 custom code troubleshooting for a particular website is beyond the scope of our support, my recommendation would be to hire a professional, who can prepare a more personalized code, after testing and reviewing your website's environment and data.
Hello again Waqar,
All posts has been clicked "Update" or even saved as new. So that´s not a problem. The posts are getting updated, but the custom code does not seem to get attached to the posts.
Should I leave the support here and give the task to a professional custom code support?
Hi Tom,
Would it be possible for you to share a clone/snapshot of your website with us?
(it can be from this website or from another test website where the issue is reproducible)
This will allow me to run some tests and troubleshot why that custom code is not working as expected on your website.
We usually recommend the free Plugin “Duplicator” (http://wordpress.org/plugins/duplicator/) for this porpoise.
If you already know how Duplicator works, you can skip the instruction video and just send me the installer file and the zipped package you downloaded.
Duplicator Instructions:
hidden link
( note: I'll need both files and you'll probably want to use DropBox, Google Drive, or similar services, as the snapshot file will be quite big )
IMPORTANT: Remember to create or keep an admin account for me before creating the snapshot, or I won’t be able to log in. You can delete the new admin account once the snapshot has been built.
I will set your next answer as private so you can provide me the information securely.
regards,
Waqar
Hi Tom,
Thanks for sharing the access details and the export file.
The login information works, but unfortunately, the export file cannot be used for testing as it doesn't fully replicate the website's environment.
I noticed that "All-in-One WP Migration" plugin is active. Do I have your permission to download the backup file from it and use it for running some tests on my own server?
Once, I'll have your confirmation, I'll proceed with the troubleshooting.
regards,
Waqar