broker_post_system.php
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
function wpdocs_updated_option( $option_name, $old_value, $value ) {
if ( $option_name === 'broker_list') {
syncBrokerListToPosts();
}
}
add_action( 'updated_option', 'wpdocs_updated_option', 10, 3 );
function syncBrokerListToPosts() {
$brokerList = getBrokerageList();
$broker_ids = [];
foreach($brokerList as $broker) {
if(!empty($broker['broker_id'])) {
$broker_ids[] = $broker['broker_id'];
}
}
global $wpdb;
$broker_posts = $wpdb->get_results('select post_author,ID from wp_posts where post_author IN('.implode(',', $broker_ids).') and post_type = "broker"',OBJECT_K);
foreach($brokerList as $bl) {
if(!empty($bl['broker_id']) && empty($broker_posts[$bl['broker_id']])) {
wp_insert_post(['post_type'=>'broker','post_author'=>$bl['broker_id'],'post_title'=>$bl['brokerage'],'post_status' => 'publish']);
} else {
wp_update_post(['ID'=>$broker_posts[$bl['broker_id']]->ID,'post_author'=>$bl['broker_id'],'post_title'=>$bl['brokerage'],'post_status' => 'publish']);
}
}
}
if( function_exists('acf_add_options_page') ) {
//Create the custom post type for brokers.. make it hidden this is just for ACF purposes
function broker_custom_post_type() {
register_post_type('broker',
array(
'labels' => array(
'name' => __('Brokers', 'textdomain'),
'singular_name' => __('Broker', 'textdomain'),
),
'public' => false,
'has_archive' => false,
)
);
}
add_action('init', 'broker_custom_post_type');
//Create the options page for the broker assignments
acf_add_options_page(array(
'page_title' => 'Broker Assignments',
'menu_title' => 'Broker Assignments',
'menu_slug' => 'broker-assignments',
'redirect' => false
));
//Create the options page for the broker assignments
acf_add_options_page(array(
'page_title' => 'Events',
'menu_title' => 'Events',
'menu_slug' => 'broker-events',
'redirect' => false
));
//Create the options page for the broker assignments
acf_add_options_page(array(
'page_title' => 'Team Assignments',
'menu_title' => 'Team Assignments',
'menu_slug' => 'team-assignments',
'redirect' => false
));
//This dynamically loads
// function be_acf_dynamic_icons( $field ) {
// if($field['name'] == 'brokerages') {
// $brokerList = getBrokerageList();
// $field['choices'] = [ 0 => 'all' ];
// foreach ($brokerList as $broker) {
// if(!empty($broker['broker_id'])) {
// $field['choices'][ $broker['broker_id'] ] = $broker['broker_id'] . " - " . $broker['brokerage'];
// }
// }
// }
// return $field;
// }
// add_filter( 'acf/load_field', 'be_acf_dynamic_icons' );
}