Skip Navigation

[Resolved] Order after numbers which are separated with spaces

This support ticket is created 6 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
- 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)

This topic contains 22 replies, has 2 voices.

Last updated by Waqar 6 years ago.

Assisted by: Waqar.

Author
Posts
#1134320

Hello again Waqar,

Yes, please do that. Didnt know All-in-One WP Migration was installed, but I guess that was the reason.

Thanks,

#1136780

Hi Tom,

Just wanted to get in touch and let you know that I haven't forgotten about this thread.

I'm still working on this and will share my detailed findings, within the next 24 hours.

Thank you for your patience.

regards,
Waqar

#1140362

Hello Waqar,

Thank you,

Just checking in to hear if you have found out a solution, or have pinpointed where the problem lays?

Looking forward to hear from you again,

#1142142

Hi Tom,

I'm really sorry to keep you waiting and unfortunately, it is still not absolutely clear why the custom code is working on my test website but not on your website's clone.

Currently, I'm testing a few alternative approaches and I'm confident that I'll have some solution or at least a workaround ready, before the end of this week.

Will update you as soon as I can and appreciate your patience.

regards,
Waqar

#1142144

Thats okay Waqar. Thanks a lot, we will be waiting for your response.

#1146340

Hi Tom,

Thank you for your patience, while I tested your website's data.

After thorough testing, I was able to make the following code work and you can replace it with the one in your theme's "functions.php" file:


/* Sortere eiendommer */
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);

        if (empty($price_1)) {
            $price_1 = 0;
        }

        if (empty($price_2)) {
            $price_2 = 0;
        }

        if (empty($price_3)) {
            $price_3 = 0;
        }
        
        // 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);

        if ($num_price_1 != 0) {
            update_post_meta( $post_id, 'wpcf-solgt-for', $num_price_1, $price_1 );
        }

        if ($num_price_2 != 0) {
            update_post_meta( $post_id, 'wpcf-overdratt-for', $num_price_2, $price_2 );
        }

        if ($num_price_3 != 0) {
            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 );

To recap, the above block will perform these operations, step-by-step:

1. When a post of type "eiendom" will be saved/updated in the admin area, it will get the values of custom fields with slugs "solgt-for", "overdratt-for" and "andel-overdratt-for".

2. If those values will have an empty space, it will be removed and the respective custom field will be updated with space-free value.

3. The sum of all these 3 fields will be calculated and will be updated in the custom field with the slug "sum".

Tip: If you're interested in a code that performs all the above actions in bulk, for all existing published "eiendom" posts at once, you can check out the code for a custom shortcode at:
hidden link

To use it, you can add this to your theme's "functions.php" file temporarily and then add the shortcode [show-price-data] in a temporary page/post and view that page/post on the front-end. You'll see the updated data in a tabular format.

Important note: Since the effect of this shortcode will be permanent and irreversible, you should only use it if you're sure that what it does is really what is required and only after making a backup copy of the database.

Once the data has been updated, please remove the shortcode from the page/post where it was added and also from the "functions.php" file.

I hope this helps and let me know how it goes.

regards,
Waqar

#1147117

Hello Waqar,

Thank you a lot for the explanation.

I will try to test with some posts before I try the bulk update. Thank you very much for the help so far with this also.

I tested, and it looks like it works correctly. Thank you very much.

Just a quick question before the bulk update, is it possible to to divide the numbers into spaces in the front-end, just for the visual aspect? Or is that a no-go?

Looking forward to hear from you again,

#1147206

Hi Tom,

Thanks for the update and I'm really glad that the code is working now.

You'll need the numeric custom fields values for the prices in the database because the operations like sorting and ordering etc cannot be effectively performed on non-numeric values which have spaces in them.

There are a few workarounds to show spaces in those values, on the front-end:

1. One option is to use two separate custom fields for each price and the accumulated sum fields.

This means that "solgt-for" field will have the numeric field stored for numerical comparisons and a new custom field e.g. "solgt-for-front" can have the number in the format with spaces, that can be used to show value on the front-end. The same can be repeated for the other two price fields and the sum field.

OR

2. The more efficient option that doesn't involve storing duplicated data would be to store only numerical values in the database but apply the desired format to those numbers at the time of content generation.

You can create a custom shortcode for that, similar to what is suggested at:
https://toolset.com/forums/topic/transforming-content-in-number-field-as-price/#post-611859

For a more personalized assistance around the custom code, you can consider hiring a professional from our list of recommended contractors at:
https://toolset.com/contractors/

regards,
Waqar