Skip Navigation

[Resolved] automatically setting the value of a custom field

This support ticket is created 4 years, 11 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 1 reply, has 2 voices.

Last updated by Beda 4 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#1509561

This is a follow-up question from this other ticket:

https://toolset.com/forums/topic/add-searchable-value-is-post-has-children/#post-1509323

I would like to add a little more logic to this code to say that
1. if the there are no children or
2. if all the children are expired then
automatically un-check the "scheduled" box (set to 0?).
Can you please help with that?
P.S.
I added the expiration date in the theme editor > functions.php file:
* Add expiration date to event custom post type
*
*/
function mycustomposttype_add_expire_date_metabox() {
add_meta_box( 'hugu_ped_expire_date_metabox', __( 'Expiration Date', 'hugu'), 'hugu_ped_expire_date_metabox_callback', array( 'mycustomposttype', 'event' ), 'side', 'high' );
}
add_action( 'add_meta_boxes', 'mycustomposttype_add_expire_date_metabox' );

Site: hidden link

#1510091

Toolset Support cannot provide Custom Code that is not related to Toolset API, and we can only limitedly help with WordPress, or PHP code that is not directly related to Toolset.

If you require professional assistance for Custom Code, we can suggest working together with a Consultant form this list https://toolset.com/contractors/

I see you use WordPress API to add a metabox with a function added to add_meta_boxes action.
https://developer.wordpress.org/reference/functions/add_meta_box/
https://developer.wordpress.org/reference/hooks/add_meta_boxes/

I understand you want to do (add this metabox) this only if certain conditions are met.
While I cannot help with adding the metabox, as that is WordPress API related, but can hint and link to DOC as above, I can help with the condition in this case - because it seems you want to check on related Posts, as related by the Toolset Types plugin.

We offer an API for this, toolset_get_related_post or toolset_get_related_posts.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

So, using that API, you can "get" related posts of a single post and - depending on what is returned - in a PHP "if()" statement, fire or abort the code that adds the metabox.

The simplest is to simply "return;" if there are no children (if toolset_get_related_posts returns empty), or/and (as you decide) the children all have an expired Custom Field set (or any data you base the condition upon)
Again here toolset_get_related_post can help, as you'll be able to "get" all that related post (or posts) data - inclusive fields or taxonomies.

You can find here many examples of how to use the API to get related posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
(Open the "More - Usage examples")

I suggest - when creating such custom code - to get familiar with the below function:

error_log(print_r($what_variable_you_want_to_analyse, true));

This code will allow you to always find what is inside the $what_variable_you_want_to_analyse, which you could always populate a line earlier in the code with

$what_variable_you_want_to_analyse = any_function_that_you_are_using();

Now, what will happen is that error_log function will put every data in the right structure into your debug log (which you can activate in WordPress using https://wordpress.org/support/article/debugging-in-wordpress/).
That helps a lot to see, what a certain code gives as data, whether it is empty, an array, a simple string, or else.

This is important, so when you craft the code, you can use this data properly.

Please let me know if you need further help with this, I can also provide you with some code examples on how to use the toolset API to get related posts, and I can always (try to) clarify any issue you might have or question-related to functions in WordPress orr PHP.

The only we cannot do is provide a ready to copy and paste custom code, already adapted to your site - that would be something the abovementioned Consultants would be a better referral.

Thanks!