Support

Account

Home Forums Backend Issues (wp-admin) Hook to be executed if a value is changed, removed or added

Solving

Hook to be executed if a value is changed, removed or added

    • xd1704

    • January 11, 2020 at 8:50 am

    I’ve three CPT Band, Album and Review

    In Album I’ve a relationship field with the Band name from the Band CPT
    (One album can have several bands, e.g. compilation)

    In Review I’ve a relationship field with the Album name
    (One album can have several reviews)

    I would like in the Review CPT to update automatically a field “review_band_name” with the Band Names “album_name” from the Album CPT if an album in the field “review_album” is selected, removed or changed.

    function acf_review_album_band_update( $field ) {
    	$field = get_field('album_band',get_field('review_album'));
    return $field;
    }
    
    add_filter('acf/update_value/name=review_album_band', 'acf_review_album_band_update');

    this code work, but logically only when the post is saved. Is there any filter which could help to update if review_album” is selected, removed or changed ?

  • If your field is a relationship or post object field the only thing that is saved in the DB is the post ID of the related post. When you use get_field() ACF does a query to get that post. If that post is changed then the change should already be reflected. If thee post is deleted then ACF should return nothing. However, if the post is in the trash ACF will still return post because it queries by the post ID and not the status.

    • xd1704

    • January 15, 2020 at 10:08 pm

    Hi John,

    it’s a field that is updated with the value of a field in an other CPT.
    therefore I use the post id store in an object relation field.

    CPT Album
    Title (album name)
    – Artist
    – Lenght
    – number of title
    – genre …

    CPT Review
    – Title (Review sentence)
    Album (object relation field pointing to CPT album)
    – Artist (updated from Albumn CPT through the object relationship field Album with Artist)

    function acf_review_album_band_update( $field ) {
    	$field = get_field('album_band',get_field('review_album'));
    return $field;
    }
    
    add_filter('acf/update_value/name=review_album_band', 'acf_review_album_band_update');

    it works, but it updates the artist name only when I publish, any chance to have something more dynamic ?

  • I’m not exactly sure what you’re trying to do. Maybe I’m dense today. Can you explain better what you want to happen?

    • xd1704

    • January 16, 2020 at 3:09 am

    CPT Album
    — – Album
    | – Artist
    | – Length
    | – number of title
    | – genre …
    |
    | CPT Review
    | – Title (Review sentence)
    — – Album
    – Artist

    I’ve got a one to many relationship between CPT Album and CPT Review wit the field Album
    I want the field Artist to be automatically updated once an album is chosen in the CPT review

  • Sorry it took so long to get back to this. I would use a bidirectional relationship field. This is not available ACF, but there are two plugins that will do this. Mine is simple https://wordpress.org/plugins/post-2-post-for-acf/. There are others with a lot more features. Or you can code it yourself, there is a guide here https://www.advancedcustomfields.com/resources/bidirectional-relationships/ as well as several others.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.