Skip Navigation

[Resolved] Split: Update the parent post, when the child post is published from the admin area

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

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 5 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#1215265

I found some resources that are aligned:

https://toolset.com/forums/topic/claim-ownership-of-a-specific-post-with-admin-approval/
https://toolset.com/forums/topic/have-a-user-claim-a-post-add-another-user-to-post-besides-the-admin/
https://toolset.com/forums/topic/trying-to-grab-parent-title-to-use-as-childs-title/

If I try and focus on just changing a "flag" on the parent post from the child when published I created:

function publish_claim( $post_ID ) {

if ( get_post_type( $post_ID ) == 'claimed-listing' ) {
$businesslisting_id = get_post_meta($post_ID, '_wpcf_belongs_business-listing_id', true);
update_post_meta( $businesslisting_id, 'wpcf-claimed-listing', '1' );
}
}
add_action( 'save_post', 'publish_claim', 99);

but I couldn't get it to work. What is difficult to know is what are the actual fields are... there doesn't seem to be an easy way to work this out?

#1215343

Hi Rudi,

Thank you for contacting us and I'll be happy to assist.

Your requirement can be split into two main parts:

1. Execution of a custom function when a child post is changed from draft to published.

2. In that custom function, you need the ID of the parent post in relationship to perform actions like changing the author and the custom field value(s).

For point 1, "save_post" action can work ( ref: https://codex.wordpress.org/Plugin_API/Action_Reference/save_post ), but you'll need additional checks to make sure that it is the post type and the post status transition, that you'd like to target.

A better alternative would be "{status}_{post_type} Hook", as explained in the following guide:
https://codex.wordpress.org/Post_Status_Transitions#.7Bstatus.7D_.7Bpost_type.7D_Hook

For example, this is the code snippet from that guide:


function on_post_publish( $ID, $post ) {
    // A function to perform actions when a post is published.
}
add_action(  'publish_post',  'on_post_publish', 10, 2 );

On your website, you'll replace "publish_post" with the actual slug of your child post e.g. "publish_claimed-listing".

To get the ID of the parent post in the relationship (point 2), you can use "toolset_get_related_posts" function, as explained at:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Once you have the ID of that parent post, you can use "update_post_meta" function ( ref: https://developer.wordpress.org/reference/functions/update_post_meta/ ) to update the custom field value(s) and the "wp_update_post" function ( ref: https://developer.wordpress.org/reference/functions/wp_update_post/ ) for the author.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1216810

The "publish_post" doesn't seem to work with toolset custom post types.

To test my theory, I used the below - which hard codes the meta data I want to change.
this will not fire when I publish the custom post type.

function on_post_publish( $ID, $post) {
// A function to perform actions when a post is published.
update_post_meta( 68012, 'wpcf-flag', '0' );
}
add_action( 'publish_claimed-listing', 'on_post_publish', 10, 2 );

if I change to this [note the change in the last line] and go a save a normal "post" it works and changes the flag field.

function on_post_publish( $ID, $post) {
// A function to perform actions when a post is published.
update_post_meta( 68012, 'wpcf-flag', '0' );
}
add_action( 'publish_post', 'on_post_publish', 10, 2 );

What would be stopping a toolset custom field from firing that action ? it seems to be related only to fields created by the plugin.

#1217493

Hi,

I've performed a few tests on my website and the "publish_{post_type_slug}" hook works as expected, with Toolset post types and custom fields.

I would recommend making sure that the post type slug "claimed-listing", custom post field slug "wpcf-flag" and the post ID "68012" that you're using in the code are correct.

It would also be a good idea to also test this with all non-Toolset plugins disabled and a default theme like Twenty Nineteen, to rule out any code conflict.

In case the issue still persists, you're welcome to share temporary admin login details in reply to this message.
(your next reply will be private)

Important note: Please make a complete backup copy of the website, before sharing the access details.

regards,
Waqar