Skip to content

Instantly share code, notes, and snippets.

@briansteeleca
Last active November 2, 2022 17:56
Show Gist options
  • Select an option

  • Save briansteeleca/2d3183cbe2ee9ccf9ca6dfb84d0903ce to your computer and use it in GitHub Desktop.

Select an option

Save briansteeleca/2d3183cbe2ee9ccf9ca6dfb84d0903ce to your computer and use it in GitHub Desktop.
ACF fields for OxyProps Portfolio WebSite - Full build Part 2 (https://www.youtube.com/watch?v=qwb8oC_veaw&t=3s)
[
{
"key": "group_636145701ece0",
"title": "Experience-Skill Relationship",
"fields": [
{
"key": "field_636148bd139e0",
"label": "Experience Skill Relationship",
"name": "experience-skill-relationship",
"aria-label": "",
"type": "relationship",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"post_type": [
"work-experience",
"skills"
],
"taxonomy": "",
"filters": [
"search",
"post_type",
"taxonomy"
],
"return_format": "object",
"min": "",
"max": "",
"elements": ""
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "work-experience"
}
],
[
{
"param": "post_type",
"operator": "==",
"value": "skills"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": true,
"description": "",
"show_in_rest": 0
},
{
"key": "group_636173472a16e",
"title": "Skills",
"fields": [
{
"key": "field_63617347f40a6",
"label": "Proficiency",
"name": "proficiency",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": ""
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "skills"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": true,
"description": "",
"show_in_rest": 0
},
{
"key": "group_636173fd2abe7",
"title": "Work Experience",
"fields": [
{
"key": "field_636173fd0e579",
"label": "Company",
"name": "company",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": ""
},
{
"key": "field_636174415bbcb",
"label": "Job Title",
"name": "job_title",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": ""
},
{
"key": "field_6361744e468a4",
"label": "Start Date",
"name": "start_date",
"aria-label": "",
"type": "date_picker",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"display_format": "d\/m\/Y",
"return_format": "F Y",
"first_day": 1
},
{
"key": "field_6361745dc9e06",
"label": "Still There",
"name": "still_there",
"aria-label": "",
"type": "true_false",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"message": "",
"default_value": 1,
"ui_on_text": "",
"ui_off_text": "",
"ui": 1
},
{
"key": "field_63617490e1993",
"label": "End Date",
"name": "end_date",
"aria-label": "",
"type": "date_picker",
"instructions": "",
"required": 0,
"conditional_logic": [
[
{
"field": "field_6361745dc9e06",
"operator": "!=",
"value": "1"
}
]
],
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"display_format": "d\/m\/Y",
"return_format": "F Y",
"first_day": 1
},
{
"key": "field_6361752762b70",
"label": "Experience Items",
"name": "experience_items",
"aria-label": "",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"layout": "block",
"pagination": 0,
"min": 0,
"max": 0,
"collapsed": "",
"button_label": "Add Row",
"rows_per_page": 20,
"sub_fields": [
{
"key": "field_6361764354da5",
"label": "Experience Item",
"name": "experience_item",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": "",
"parent_repeater": "field_6361752762b70"
}
]
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "work-experience"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": true,
"description": "",
"show_in_rest": 0
}
]
<?php
// This goes in a Bricks Code element
$string_to_output = '';
$still_there = get_field('still_there');
$start_date = get_field('start_date');
$end_date = get_field('end_date');
if( $still_there ):
if( $start_date ):
$string_to_output .= 'From ' . $start_date . ' to present';
endif;
else:
if( $start_date && $end_date ):
$string_to_output .= 'From ' . $start_date . ' to ' . $end_date;
endif;
endif;
echo $string_to_output;
?>
<?php
// Source:
// https://www.advancedcustomfields.com/resources/bidirectional-relationships/
// Tutorial (Oxygen)
// Video tutorial: https://www.youtube.com/watch?v=oHFa7QwuPgU
function bidirectional_acf_update_value($value, $post_id, $field)
{
// vars
$field_name = $field["name"];
$field_key = $field["key"];
$global_name = "is_updating_" . $field_name;
// bail early if this filter was triggered from the update_field() function called within the loop below
// - this prevents an inifinte loop
if (!empty($GLOBALS[$global_name])) {
return $value;
}
// set global variable to avoid inifite loop
// - could also remove_filter() then add_filter() again, but this is simpler
$GLOBALS[$global_name] = 1;
// loop over selected posts and add this $post_id
if (is_array($value)) {
foreach ($value as $post_id2) {
// load existing related posts
$value2 = get_field($field_name, $post_id2, false);
// allow for selected posts to not contain a value
if (empty($value2)) {
$value2 = [];
}
// bail early if the current $post_id is already found in selected post's $value2
if (in_array($post_id, $value2)) {
continue;
}
// append the current $post_id to the selected post's 'related_posts' value
$value2[] = $post_id;
// update the selected post's value (use field's key for performance)
update_field($field_key, $value2, $post_id2);
}
}
// find posts which have been removed
$old_value = get_field($field_name, $post_id, false);
if (is_array($old_value)) {
foreach ($old_value as $post_id2) {
// bail early if this value has not been removed
if (is_array($value) && in_array($post_id2, $value)) {
continue;
}
// load existing related posts
$value2 = get_field($field_name, $post_id2, false);
// bail early if no value
if (empty($value2)) {
continue;
}
// find the position of $post_id within $value2 so we can remove it
$pos = array_search($post_id, $value2);
// remove
unset($value2[$pos]);
// update the un-selected post's value (use field's key for performance)
update_field($field_key, $value2, $post_id2);
}
}
// reset global varibale to allow this filter to function as per normal
$GLOBALS[$global_name] = 0;
// return
return $value;
}
add_filter(
"acf/update_value/name=experience-skill-relationship",
"bidirectional_acf_update_value",
10,
3
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment