Skip Navigation

[Resolved] Define Custom Fields Value based on other Custom Fields

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 4 replies, has 2 voices.

Last updated by Nigel 5 years ago.

Assigned support staff: Nigel.

Author
Posts
#427885

In order to handle sorting on multiple Cust Fields, I would like to add a separate field that is filled based on the value of other fields.

I have created the following function:

// CREATE UNIQUE TITLE FOR CPT 'Opdrachten' - checklisten
   add_action('cred_save_data','content_sorttitle',10,2);

function content_sorttitle($post_id, $form_data) {
{
    $type = get_post_type($post_id, $form_data);
    if (($type == 'post')||($type == 'page')||($type == 'portfolio'))
    {
        $platform = get_post_meta($post_id, 'wpcf-content-platform', true) * 10000;
        $type = get_post_meta($post_id, 'wpcf-content-type', true) * 100;
        $lob = get_post_meta($post_id, 'wpcf-content-lob', true);
//        $key = $platform . '-' . $type . '-' . $lob . '-'. '-ID' . $post_id;
        $key = $platform + $type + $lob; 
	    update_post_meta($post_id, 'wpcf-content-sort-title', $key);
    }
  }
}

This function is working fine when I update the entries via CRED-Form.

What do I need to change to activate this function for the following situations:
1) Editing Post via Admin way instead of Cred Forms
2) Using Bulk-edit option in Admin menu

#427999

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Stefan

I'm handling your other ticket which is related to the same topic.

Can I check with you where you are at in terms of which solution you prefer/require so that I can give you a complete answer in one go.

You can answer here or in the other thread.

Thanks

Nigel

#428026

As I said, I do know how to handle this via CRED-Forms and I would like to know what change is necessary to get a field updated based on a certain logic after a regular admin edit of Post, Page or Portfolio or via Bul-Edit Admin menu.

#428595

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Stefan

I posted the reply here against the wrong ticket. I have added it to your other ticket and will update this one shortly...

#428604

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Stefan

You can have a universal solution by using the built-in WordPress hook save_post (https://codex.wordpress.org/Plugin_API/Action_Reference/save_post).

Functions hooked onto this action will run whenever a post (in the general WordPress sense, i.e. including pages and custom post types) is posted or updated.

If you use this hook you won't need a separate solution that runs after your CRED form posts. Because the hook runs immediately after the post is saved you can retrieve the post meta, construct your new custom field, and set it.

I think you should be able to adapt your code sample above to work, but if you need help, please ask.

Note that in your code you pass two arguments to get_post_type but it only takes one (the post id).