How to link custom field of two custom post types?

admin2025-06-07  31

I have two custom posts

Custom post-type A  -> Custom field "enable or disable" 
Custom post-type B  -> Custom field "enable or disable"

I am using the ACF for making the custom fields I want to link both custom post types... if enabled in custom post type B then enable it in custom post type A. So basically i want custom post type A to be controlled by B .

Is it possible to link? How can i achieve this? Pls help Thanks

I have two custom posts

Custom post-type A  -> Custom field "enable or disable" 
Custom post-type B  -> Custom field "enable or disable"

I am using the ACF for making the custom fields I want to link both custom post types... if enabled in custom post type B then enable it in custom post type A. So basically i want custom post type A to be controlled by B .

Is it possible to link? How can i achieve this? Pls help Thanks

Share Improve this question edited Oct 16, 2018 at 14:09 asked Oct 16, 2018 at 13:14 user145078user145078 2
  • Why not add it to an options page? It would make more sense (to me, at least, from what I've read) to have an options page which controls the one field, across both post tyes ... why do you need to control the field from the post-type itself? – admcfajn Commented Oct 16, 2018 at 14:52
  • @admcfajn because options are fixed ..i need dynamic fields – user145078 Commented Oct 16, 2018 at 15:12
Add a comment  | 

1 Answer 1

Reset to default 0

Can you please elaborate some more? Is this custom field possibly an ACF field? For what is this custom field tied, for a single custom post? And should this linking impact all of the posts in both post types?

(Sorry, but I don't have rights to make comments yet)

Supposing all of that, what I would suggest and probably do, is to create an ACF Options page and add this custom field there.

if( function_exists('acf_add_options_page') ) {     
    acf_add_options_page(array(
        'page_title' => 'Options'
    ));     
}

And then you can easily add the check for this field value on any place using function $value = get_field( 'your_custom_field', 'options' );


If what I supposed is wrong, please add some comments and correct me. There is a possibility to do exactly what you asked by using action hook save_post, but I just don't think that would be such a great idea. You would have to take the custom field value from the current post, and then loop through all of the posts in both post types and alter the value there as well.


Update:

Since I got it all wrong what was the intention of the OP, which was actually this:

How to update the value of one ACF field when updating another

Here is the correct answer. Use the acf/update_value hook, in combination with update_field() function:

function wpse316844_on_custom_field_save( $value, $post_id, $field  ) {

    // do some checks to see if the field is the one you want

    update_field( 'another_custom_field', $value, $other_post_id )

}

add_filter('acf/update_value/name=custom_field_name', 'wpse316844_on_custom_field_save', 10, 3);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749255943a317658.html

最新回复(0)