Skip Navigation

[Resolved] How to change the publishing date in the parent post if there is a child?

This support ticket is created 8 years, 2 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by mcjS 8 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#370491

In my website I have a parent custom post type (advertise) which could have a child (booking).
When there is a child (booking) I want to change the publishing date in the parent post (advertise) into "today" and the publishing time into "now".

I think I'm looking for extra coding in my functions.php to realise this.

And I think this is the way:

For each booking
get the parent
change the publishing date advertise into today
change the publishing time advertise into now
save parent
end bookings.

Can you give me some help to write this piece of coding? Or an example I can follow?

#370539

Dear mcjS,

Here is a demo codes, for your reference:

function update_parent_publish_time_func($post_id) {
	if ( wp_is_post_revision( $post_id ) && get_post_type($post_id) != 'booking') 
		return;
	
	$parent_id = get_post_meta($post_id, '_wpcf_belongs_advertise_id', true);
	if(get_post_type($parent_id) == 'advertise'){
		$update_args['ID'] = $parent_id;
		$update_args['post_date'] = date('Y-m-d H:i:s', strtotime('Now'));
		
		remove_action('save_post', 'update_parent_publish_time_func');
		$res = wp_update_post( $update_args );
		// re-hook this function
		add_action('save_post', 'wpml_set_post_date_gmt');
	}
}
add_action( 'save_post', 'update_parent_publish_time_func' );
#370565
database.png
child-parent-relation.jpg

It isn't working, maybe I do something wrong?

this is the code I made in functions.php

***

function update_parent_publish_time_func($post_id) {
if ( wp_is_post_revision( $post_id ) && get_post_type($post_id) != 'aanmelding')
return;

$parent_id = get_post_meta($post_id, '_wpcf_belongs_advertentie_id', true);
if(get_post_type($parent_id) == 'advertentie'){
$update_args['ID'] = $parent_id;
$update_args['post_date'] = date('Y-m-d H:i:s', strtotime('Now'));

remove_action('save_post', 'update_parent_publish_time_func');
$res = wp_update_post( $update_args );
// re-hook this function
add_action('save_post', 'wpml_set_post_date_gmt');
}
}
add_action( 'save_post', 'update_parent_publish_time_func' );

***

(for your information: booking is CPT aanmelding and advertise is CPT advertentie (dutch) )

There is no _wpcf_belongs_advertentie_id in the database?

see print screens from the child with post id 8525.

#370815

Since it is a custom PHP codes problem, could you duplicate same problem in a test site and fill below private detail box with login details, also point out the problem page URL, and where I can edit your PHP codes, I need a live website to test and debug this problem, thanks

#370894

Thanks for the details, the PHP codes works only when you publish the "aanmelding" post, I just tested publish the "aanmelding" post:
hidden link

And it's parent advertentie post does correctly update the publish time to now:
hidden link

Please test again, I did not do any other modification in your website.

#370907
time.png
SCREEN3.png

hidden link
indeed, when there is a new "aanmelding" it changes the publishing date.

3 questions:

1. When i want from the dashboard to delete another advertentie (ad) it is duplicating a part of this CPT?
(see screen3.png)

2. how to change the GMT time into GMT + 1 (i have these settings in the wordpress instalation) = Amsterdam time zone. (see time.png)

3. But: What my client wants, is not just one time changing the publishing date of CPT advertentie, but it has to change every time when someone visits the website. So that these ads are always on top, when there is a child.
This was what i meant with:
For each booking/aanmelding
- get the parent
- change the publishing date advertise into today
- change the publishing time advertise into now
end bookings/aanmeldingen.

Is this possible?

#371319

What you request is out the range of Toolset, the codes I provided is just a demo for you,
in your case, I suggest create a cron job in your website:
https://codex.wordpress.org/Function_Reference/wp_schedule_event

And I suggest you check out our certified partners for it:
https://toolset.com/consultant/

#371324

tnx for the information.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.