Skip Navigation

[Fermé] parent/child post creation, what hook to use?

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.

This topic contains 6 réponses, has 2 voix.

Last updated by Caridad Il y a 8 années et 1 mois.

Assigned support staff: Caridad.

Auteur
Publications
#167438

I have made parent/child relation in Types. So I can create child post types within the parent post type creation form.
Child posts have select field. I would like to do some action based on the value of that field (change user meta for example, but it doesnt matter).
What hook should I use for my function ?

#167465

Dear Darius,

The relationship is stored as a custom field in the child. Its called '_wpcf_belongs_parent_id', replacing 'parent' with the slug of your custom post type. So if you parent was to be 'house', the code would look:

add_filters( "update_post_metadata", "on_relationship", 10, 5 );
function on_relationship( $null, $object_id, $meta_key, $meta_value, $prev_value ) {
   // do whatever you want here

  // return null for saving to continue
  return null;
}

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Regards,
Caridad

#167474

What your code has to do with 'house'?
I have never used filters in wordpress so just to be clear:

I want to perform an action based on the select field value of newly created post (creating was done in parent post type form).
Will I be able to do that with your code?
Does arguments of on_relationship function need to be passed by my, or wordpress does it and I can use them inside the function?

#167572

Dear Darius,

You dont specify what your parent or your child is, so I used 'house' as and example. In my example, 'house' is the parent post type. You have to change that to your parent post type slug.

To learn about filters, you can go to this address:
http://codex.wordpress.org/Plugin_API#Create_a_Filter_Function

Just keep in mind that this is a special filter, you have to return null if you want the value to be saved.

The arguments are passed to the function by WordPress, you can use those arguments to check if you are updating _wpcf_belongs_house_id and add your code:

add_filters( "update_post_metadata", "on_relationship", 10, 5 );
function on_relationship( $null, $object_id, $meta_key, $meta_value, $prev_value ) {
if ( $meta_key == '_wpcf_belongs_house_id') {
// do whatever you want here
// ...
}
// return null for saving to continue
return null;
}

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Regards,
Caridad

#168352

Ok, one more thing:
Let's say that created child post has select field associeted with meta field "wpcf-value". Will function on_relationship have acces to this value? Will I be able to do something like this:
function on_relationship( $null, $object_id, $meta_key, $meta_value, $prev_value ) {

if ( $meta_key == '_wpcf_belongs_house_id') {
$value = (get "wpcf-value" somehow);
if($value=='1')
{
//do some stuff
}
}
// return null for saving to continue
return null;
}

If so, how to get value of a filed "wpcf-value"?

#168355

I just realized that post is not saved yet, therefor there will be no meta named "wpcf-value". So the question becomes: how to intercept value from a select field inside on_relationship function?

#168676

Dear Darius,

The post should be saved by now, you can do this to get the value of a custom field.

$value = get_post_meta( $object_id, 'wpcf-value', true );

If you find that this field hasn't been saved yet, try increasing the priority parameter:

add_filters( "update_post_metadata", "on_relationship", 999, 5 );

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Regards,
Caridad

Le sujet ‘[Fermé] parent/child post creation, what hook to use?’ est fermé à de nouvelles réponses.