admin管理员组文章数量:1130349
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
1 Answer
Reset to default 0Can 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);
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
1 Answer
Reset to default 0Can 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);
本文标签: How to link custom field of two custom post types
版权声明:本文标题:How to link custom field of two custom post types? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749255918a2340470.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论