Skip Navigation

[Resolved] Want to Store Value of Three Conditional Custom fields data

This support ticket is created 4 years, 10 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Shane 4 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1501719

Hi there
I have three Custom fields which are basically dropdown fields for only one purpose. I have actually a list of more than 40 Dropdown values which should be filtered based on the selection of a radio custom field. So in Radio field there are three options and based on each option selection, This dropdown field values should be filtered and display only required values in the dropdown, But for this specific functionality, I tried to make it work using Javascript but thats not working good enough, So I want to add three different custom dropdown fields with condition to be displayed as per the selection on the previous Radio field. But Now the problem is that, I want to store the value of any of these three dropdown fields to a default custom field. Its like a Variable which will be automatically updated based on the value on any of these three fields

I have copied a code from your Support forum but thats not working.

add_filter('save_post', 'combine_my_fields');
function combine_my_fields($post_id, $post) {
$build_type = get_post_meta($post_id, 'wpcf-build_resident', true);
$build_type .= get_post_meta($post_id, 'wpcf-build-commerce', true);
$build_type .= get_post_meta($post_id, 'wpcf-build-land', true);
update_post_meta($post_id, 'building-property-type', $build_type);

building-property-type is the main field where I want to save the data from any of the above custom fields 1: wpcf-build_resident 2: wpcf-build-commerce 3: wpcf-build-land

Can you please guide me how to store data of these fields to the above custom field

Thank you

#1501791

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Manish,

Thank you for getting in touch.

Is this for backend functionality or frontend ?

Please let me know.
Thanks,
Shane

#1501797

This functionality should work on the backend. In the frontend, I will display these three custom fields as per the conditional input. For example, If someone selects Residential in the Radio Field then I will display Dropdown "wpcf-build_resident" dropdown field. And the selected value will automatically get updated on building-property-type custom field. And then the value of building-property-type will be displayed in post page.

So this is what I am trying to do.

#1501855

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Manish,

Could you try the code like this.

// this function makes all posts in the default category private
 
function combine_my_fields($post_id) {
   
        remove_action( 'save_post', 'combine_my_fields' );
 $build_type = get_post_meta($post_id, 'wpcf-build_resident', true);
$build_type .= get_post_meta($post_id, 'wpcf-build-commerce', true);
$build_type .= get_post_meta($post_id, 'wpcf-build-land', true);
update_post_meta($post_id, 'building-property-type', $build_type);

        // re-hook this function
        add_action( 'save_post', 'set_private_categories' );
    }
}
add_action( 'save_post', 'combine_my_fields' );

Please let me know if this helps.
Thanks,
Shane

#1501977

Shane
I am a bit confused with the comment over here in the function, As it says that this function makes the post in default category private. So I don't understand what does it mean.

And for your information, This function will only be applicable on a custom post type "property " and these custom fields are attached to this custom post type. and if this function will make private to all the posts then I don't need to implement this function. So Please explain what does it mean.

#1502027

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Manish,

The code was copied from the wordpress repository as a base

  
function combine_my_fields($post_id) {
    if (get_post_type($post_id) == 'property'){
        remove_action( 'save_post', 'combine_my_fields' );
 $build_type = get_post_meta($post_id, 'wpcf-build_resident', true);
$build_type .= get_post_meta($post_id, 'wpcf-build-commerce', true);
$build_type .= get_post_meta($post_id, 'wpcf-build-land', true);
update_post_meta($post_id, 'building-property-type', $build_type);
   // re-hook this function
        add_action( 'save_post', 'set_private_categories' );
 }
     
    }

add_action( 'save_post', 'combine_my_fields' );

Please try this above now.

Thanks,
Shane

#1512239

Hey Guys, I appreciate your help but this code you sent again is not working. Can you show me where to add them, In the Function.php or Toolset functions area?

And Can you confirm if "building-property-type" field should a normal text field instead of a select field? Because for now its a select field with a full list of option values that are divided into three 3 conditional dropdowns.

Please watch this video to understand exact problem.
hidden link

#1513127

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Manish,

I see another issue. Given that its a types field you are updating it should be wpcf-building-property-type.

So making adjustments to the code it should be.


function combine_my_fields($post_id) {
    if (get_post_type($post_id) == 'property'){
        remove_action( 'save_post', 'combine_my_fields' );
 $build_type = get_post_meta($post_id, 'wpcf-build_resident', true);
$build_type .= get_post_meta($post_id, 'wpcf-build-commerce', true);
$build_type .= get_post_meta($post_id, 'wpcf-build-land', true);
update_post_meta($post_id, 'wpcf-building-property-type', $build_type);
   // re-hook this function
        add_action( 'save_post', 'set_private_categories' );
 }
      
    }
 
add_action( 'save_post', 'combine_my_fields' );

Also confirming that the fields must be a single line field.

Finally you can go to Toolset -> Custom Code -> Add new and then add the custom code. Ensure that the custom code is set to active.

Thanks,
Shane

#1514833

Its still not working. No update is being taken and the values of these dropdown fields are not being inserted in that custom field.

Can you please have a look to fix it?

#1515143

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Manish,

Would you mind allowing me to have admin access to the site so that i can check on this one for you ?

The private fields have been enabled for your next response.

Also please send me a link to a page where I can do the testing on this.

Thanks,
Shane