Skip Navigation

[Resolved] add searchable value is post has children

This support ticket is created 5 years, 6 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 9 replies, has 2 voices.

Last updated by Purchasing WCER 5 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#1281345

Tell us what you are trying to do?

I have "workshop" parents with "scheduled event" children. I would like to have a search page where I can filter on scheduled events by the "workshop" parent. So, I need some way to tag the parent "workshop" as scheduled IF it has 1 or more children and remove that tag when those children expire or are deleted.

Is there any documentation that you are following? couldn't find anything relevant

What is the link to your site? hidden link
hidden link

#1281523

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you try to save the tag for the parent when you save the child post. That means when you save child post that means parent has child and you should update the field for the parent accordingly.

You should try to use the save_post action hook:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

#1282079

Here is what I tried. It isn't working. I also would like to add a way to say that, if the only assigned event post is expired then set the scheduled field to unchecked.

Parent post type slug = workshop
Child post type slug = event
Workshop custom field check box slug = scheduled

//Auto mark workshops as scheduled if they have an an event child that isn't expired
function is_scheduled( $post_id, $post ){
if ( 'workshop' == $post->post_type ) {
$child_posts = types_child_posts('event');
if ($child_posts) {
return true;
}
update_post_meta( $post_id, 'wpcf-scheduled', 1 );
}
}
add_action( 'save_post', 'is_scheduled', 30, 2 );

#1282513

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share the problem URL and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1287077

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Unfortunately, the wp-admin access details you send to me is not working at this end.

Can you please send me working access details.

I have set the next reply to private which means only you and I have access to it.

#1287745

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Toolset offers a place where you can add the custom code:
=> hidden link

But when I try to add new code snippet it gives me the following error:

Unable to create the base directory for code snippets "wp-content/toolset-customizations". Please check permissions on the server.

Can you please give permission or send me FTP access details.

More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

I have set the next reply to private which means only you and I have access to it.

#1288947

The "toolset-customizations" directory has been made writeable from the web. Please try again.

#1289337

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now. I've added the following code to the "Custom Code" section:
=> hidden link

//Auto mark workshops as scheduled if they have an an event child that isn't expired
function func_is_scheduled( $post_id, $post ){
if ( 'workshop' == $post->post_type ) {

			$child_posts = toolset_get_related_posts($post_id,'workshop-info-event','parent',999,0,array(),'post_id','child');
  			
   			if(count($child_posts) > 0){
     			   update_post_meta( $post_id,'wpcf-scheduled',1);
   			}  
  			
 }
}
add_action( 'save_post', 'func_is_scheduled', 100, 3 );

For the testing purpose, the following Workshop has the event, you should uncheck the "Scheduled" checkbox and save the post, as this workshop has one event available the checkbox will be automatically checked.
=> hidden link

#1290059

Wonderful! Thank you so much for the help!

#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' );