acf two wa relation hip - cfp wilbrod bherer · dwelcome to disqus!discover more great discussions...

9
ACF Two Wa Relationhip Poted Neil Harlow (http://plu.google.com/112355556396352267792?rel=author), Y-Deign, Inc How to modif the WordPre ACF plugin to allow for two wa relationhip. The ACF Relationhip (http://www.advancedcutomfield.com/reource/relationhip/) field i great for creating advanced relationhip etween pot. If we want to create a relationhip from pot A to pot we add it on the pot edit page. nglih (http://-deign.com/log/acf-two-wa-relationhip/) nglih (http://-deign.com/log/acf-two-wa-relationhip/)

Upload: others

Post on 06-Apr-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ACF Two Wa Relation hip - Cfp Wilbrod Bherer · dWelcome to Disqus!Discover more great discussions just like this one. We're a lot more than comments. iÌÊ-Ì>ÀÌi` Dismiss × Node

ACF Two Wa RelationhipPoted  Neil Harlow (http://plu.google.com/112355556396352267792?rel=author), Y-Deign,Inc

How to modif the WordPre ACF plugin to allow for two wa relationhip.

The ACF Relationhip (http://www.advancedcutomfield.com/reource/relationhip/) fieldi great for creating advanced relationhip etween pot. If we want to create a relationhipfrom pot A to pot  we add it on the pot edit page.

nglih (http://-deign.com/log/acf-two-wa-relationhip/)nglih (http://-deign.com/log/acf-two-wa-relationhip/)

Page 2: ACF Two Wa Relation hip - Cfp Wilbrod Bherer · dWelcome to Disqus!Discover more great discussions just like this one. We're a lot more than comments. iÌÊ-Ì>ÀÌi` Dismiss × Node

(http://-deign.com/wp-content/upload/2015/10/creen-hot-2015-10-28-at-11.45.12-AM.png)

Thi i great, ut what ou might e after i a two-wa relationhip. When ou add therelationhip from A→, ou would alo like there to e a relationhip from →A. If ou gocheck out pot , ou will ee that there i no uch relationhip (et). You can add it ourelf, utthi ecome unmanageale and inefficient at ome point. All we need i the following:

When we add a relationhip from pot A→, create the invere (→A).When we remove one of the relationhip (A→ or →A), remove the invere.

The firt tep i to add the following to our function.php file within our theme director.The acf/ave_pot hook(http://www.advancedcutomfield.com/reource/acfave_pot/) let u handle thingefore an ACF field are actuall updated. The third parameter (priorit) let u hook in eforethe ave occur. We have to handle efore o that we can check for an dropped relationhip.

function acf_pre_save_hook( $post_id ) { // TODO } add_action( 'acf/save_post', 'acf_pre_save_hook', 1 ); // run before ACF saves the $_POST['acf'] data

And here i what the overall code look like:

Page 3: ACF Two Wa Relation hip - Cfp Wilbrod Bherer · dWelcome to Disqus!Discover more great discussions just like this one. We're a lot more than comments. iÌÊ-Ì>ÀÌi` Dismiss × Node

// ============================== ACF pre-save hook ============================= // // We needed this to simulate a two-way relationship (acf doesnt do this apparently) function acf_pre_save_hook( $post_id ) { // bail early if no ACF data if ( empty( $_POST['acf'] ) ) { return; } $fields = $_POST['acf']; $relationship_field = 'field_111806f5651b7'; // This is the id of the acf relationship field if ( isset( $fields[ $relationship_field ] ) ) { $post_id = $_POST[ 'post_ID' ]; $previous_relations = get_field( $relationship_field, $post_id, false ); // 1. Attach the opposite relations $target_relationships = $fields[ $relationship_field ]; if ( !is_array( $target_relationships ) ) $target_relationships = array(); // there were no values, lets just assign empty array for ($i = 0; $i < count( $target_relationships ); $i++) { $existing_relationships = get_field( $relationship_field, $target_relationships[ $i ], false ); // false means just return the array of ids if ( $existing_relationships == null ) $existing_relationships = array(); array_push( $existing_relationships, $post_id ); update_field( $relationship_field, $existing_relationships, $target_relationships[ $i ] ); } // 2. If any relationships were dropped, we need to update those other posts with this info (ie: remove that relation on other end). $removed_relationships = array(); for ($i = 0; $i < count( $previous_relations ); $i++) { if ( !in_array( $previous_relations[ $i ], $target_relationships ) ) { array_push( $removed_relationships, $previous_relations[ $i ] ); } } for ($i = 0; $i < count( $removed_relationships ); $i++) { // Fetch the relationship field for the target post $product_groups = get_field( $relationship_field, $removed_relationships[ $i ], false ); if ( is_array( $product_groups ) ) { // Remove the current post from that array if ( ( $key = array_search( $post_id, $product_groups ) ) !== false ) { unset( $product_groups[ $key ] );

Page 4: ACF Two Wa Relation hip - Cfp Wilbrod Bherer · dWelcome to Disqus!Discover more great discussions just like this one. We're a lot more than comments. iÌÊ-Ì>ÀÌi` Dismiss × Node

R L A T D   P O T

} // Update that field with subset update_field( $relationship_field, $product_groups, $removed_relationships[ $i ] ); } } } } add_action( 'acf/save_post', 'acf_pre_save_hook', 1 ); // run before ACF saves the $_POST['acf'] data

The logic i prett traight-forward on thi one, it jut take a it of work. We ued thi particularolution to group pot together via ACF. Keep in mind that we are impl updating meta data,o thi hould work acro different pot tpe and page. If ou pot an area forimprovement on thi one let u know!

hare with friend

Page 5: ACF Two Wa Relation hip - Cfp Wilbrod Bherer · dWelcome to Disqus!Discover more great discussions just like this one. We're a lot more than comments. iÌÊ-Ì>ÀÌi` Dismiss × Node

WP API Multiple Taxonom Querefore we get tarted, ou will need to e running at leat the following: WordPre: 4.4+ WP RT A(http://-deign.com/log/wp-api-multiple-taxonom-quer/)

Learning Nooku Framework: etting it up.Nooku Framework i a ig tep forward taken  a team of developer to make Joomla Development eaie(http://-deign.com/log/learning-nooku-framework-etting-it-up/)

Page 6: ACF Two Wa Relation hip - Cfp Wilbrod Bherer · dWelcome to Disqus!Discover more great discussions just like this one. We're a lot more than comments. iÌÊ-Ì>ÀÌi` Dismiss × Node

PHP CM Comparion 2015(or that time I compared 7 PHP Content Management tem) Yup.  I’m here to compare PHP CMe(http://-deign.com/log/php-cm-comparion-2015/)

WordPre Development: VVV, Conitenc and AutomatedDeplomentAt Y-Deign, a with other firm in our indutr, we are often forced to develop client weite u(http://-deign.com/log/wordpre-development-conitenc-and-deploment/)

Page 7: ACF Two Wa Relation hip - Cfp Wilbrod Bherer · dWelcome to Disqus!Discover more great discussions just like this one. We're a lot more than comments. iÌÊ-Ì>ÀÌi` Dismiss × Node

dWelcome to Disqus! Discover more great discussions just like thisone. We're a lot more than comments.

Get Started

Dismiss ×

Node CMS Comparison 2015 - Y-Designs,Inc5 comments • 8 months ago

David Herron — AkashaCMS -http://akashacms.com - It produces staticwebsites while following some techniquesfrom dynamic CMS's and using modern

ACF and Yoast SEO integration1 comment • 8 months ago

Dominik Kucharski — Thank you a lot!

Joomla and its issues: Marketing,Branding, UI/UX, Extensions andDocumentation36 comments • 3 years ago

Fotis Evangelou — The problem with Joomlais that it's got either designers (who knowjack shit about the underlying code or innerworkings of Joomla, most times they haven't

Intro to K2 Templating - Part 13 comments • 2 years ago

Patricia Espinoza — Hi Max :D I'm trying toextend the registration form by adding newfields but i can't find the insert to thedatabase, so i can't save them :( Any

ALSO ON Y-DESIGNS,INC

0 Comments Y-Designs,Inc Vincent Morel

Share⤤ Sort by Best

Start the discussion…

Be the first to comment.

WHAT'S THIS?

Subscribe✉ Add Disqus to your site Add Disqus Addd Privacy�

Recommend

Contact U.

Page 8: ACF Two Wa Relation hip - Cfp Wilbrod Bherer · dWelcome to Disqus!Discover more great discussions just like this one. We're a lot more than comments. iÌÊ-Ì>ÀÌi` Dismiss × Node

(http://www.faceook.com/YDeignInc)

(http://twitter.com/intent/follow?ource=followutton&variant=1.0&creen_name=deattle)

(http://plu.google.com/+Y-deign)

(http://www.linkedin.com/compan/-deign-inc-)

(http://www.pinteret.com/deigninc)

(http://intagram.com/deattle) (http://-deign.tumlr.com)

Y-Deign, Inc

P.O. ox  22189

eattle, WA 98102

 

206.802.4492

[email protected] (mailto:[email protected])

Follow u

Name mail

uject

Page 9: ACF Two Wa Relation hip - Cfp Wilbrod Bherer · dWelcome to Disqus!Discover more great discussions just like this one. We're a lot more than comments. iÌÊ-Ì>ÀÌi` Dismiss × Node

Meage

end