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?
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' );
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.
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
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.
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?
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/