Tell us what you are trying to do?
==> Setting a Custom Field Condition programmatically
Is there any documentation that you are following?
==> None, since there is neither an API nor a DOC, however I have the requirement.
Is there a similar example that we can see?
==> Yes, see code below
I successfully create some complex Custom Fields upon the activation of a Plugin I created.
This works fine but I want to add a conditional to 2 of those automatically created fields, and of course I'd like to do that automatically as well.
So I set a condition for the fields when registering them (note, this is excerpt from larger code):
...
$conditional_display = array(
'relation' => 'AND',
'conditions' => array(
uniqid('condition_') => array(//Createa Unique ID. Toolset uses some hash here, but it shouldn't really matter.
'field' => 'Exclude from SiteMap',//The field that sets the condition
'operation' => '<>',//Not equal
'value' => '1',//Value to compare to
'month' => '03',//Why, I am not sure, but Toolset fields add a date here to the conditional so I just add it as well.
'date' => '29',
'year' => '2021',
),
),
'custom' => null,//Toolset default is empty here so I assume I shouldn't fill it either.
'custom_use' => 0,
);
...
Then I pass this conditional setting to the data array of WPCF Fields like so (again, excerpt from larger code):
...
$this->plugin_name .'-frequency' => [
'type' => 'select',
'name' => 'Update Frequency',
'data' => [
'options' => $frequency_options,
'conditional_display' => $conditional_display,
'submit-key' => 'select-frequency-always',
'disabled_by_type' => 0
],
'description' => 'Sitemap XML Frequency this Post will be updated with (how often do you plan to update this post)'
],
...
Save the fields with
...
wpcf_admin_fields_save_fields( $fields );
...
Obviously above is part of a much larger code (https://pastebin.com/WF8U4H11) but it should illustrate what I am doing.
The Condition is well set in the GUI but it does not apply (see also screenshots)
Perhaps you can point me to what I am missing.
The main problem seems to be that when we save a condition in the GUI, it triggers something else in the background additional to what is stored in PHP, which makes the magic work, and which is what I miss.
However, I see zero difference when I var_dump the fields settings. Both, wether saved with custom code or types GUI, have the precise same values in the conditionals settings.
So I assume it should be working, but it is not.
Perhaps a Developer can hint towards the right function to use additionally, as it would be really nice to add this feature to a plugin I am making for SEO Toolset Add-on.
If its not possible to hint I also understand. I tried to have a look at the API here as well but I think it is not useful for this kind of things?
https://toolset.com/documentation/programmer-reference/toolset-custom-field-api/
It seems to be a complex way to get fields, but that I can do already with simple get_post_meta and get_posts, whereas setting the fields is more complex 🙂
I would appreciate any hint as of why my Conditional is not "working"