acf_field_taxonomy

The Advanced Custom Fields acf field taxonomy class.

Defined (1)

The class is defined in the following location(s).

/core/fields/taxonomy.php  
  1. class acf_field_taxonomy extends acf_field 
  2. /** 
  3. * __construct 
  4. * Set name / label needed for actions / filters 
  5. * @since 3.6 
  6. * @date 23/01/13 
  7. */ 
  8.  
  9. function __construct() 
  10. // vars 
  11. $this->name = 'taxonomy'; 
  12. $this->label = __("Taxonomy", 'acf'); 
  13. $this->category = __("Relational", 'acf'); 
  14. $this->defaults = array( 
  15. 'taxonomy' => 'category',  
  16. 'field_type' => 'checkbox',  
  17. 'allow_null' => 0,  
  18. 'load_save_terms' => 0,  
  19. 'multiple' => 0,  
  20. 'return_format' => 'id' 
  21. ); 
  22.  
  23.  
  24. // do not delete! 
  25. parent::__construct(); 
  26.  
  27.  
  28.  
  29. /** 
  30. * get_terms 
  31. * This function will return an array of terms for a given field value 
  32. * @type function 
  33. * @date 13/06/2014 
  34. * @since 5.0.0 
  35. * @param $value (array) 
  36. * @return $value 
  37. */ 
  38.  
  39. function get_terms( $value, $taxonomy = 'category' ) { 
  40.  
  41. // load terms in 1 query to save multiple DB calls from following code 
  42. if( count($value) > 1 ) { 
  43.  
  44. $terms = get_terms($taxonomy, array( 
  45. 'hide_empty' => false,  
  46. 'include' => $value,  
  47. )); 
  48.  
  49.  
  50.  
  51. // update value to include $post 
  52. foreach( array_keys($value) as $i ) { 
  53.  
  54. $value[ $i ] = get_term( $value[ $i ], $taxonomy ); 
  55.  
  56.  
  57.  
  58. // filter out null values 
  59. $value = array_filter($value); 
  60.  
  61.  
  62. // return 
  63. return $value; 
  64.  
  65.  
  66. /** 
  67. * load_value() 
  68. * This filter is appied to the $value after it is loaded from the db 
  69. * @type filter 
  70. * @since 3.6 
  71. * @date 23/01/13 
  72. * @param $value - the value found in the database 
  73. * @param $post_id - the $post_id from which the value was loaded from 
  74. * @param $field - the field array holding all the field options 
  75. * @return $value - the value to be saved in te database 
  76. */ 
  77.  
  78. function load_value( $value, $post_id, $field ) { 
  79.  
  80. // get valid terms 
  81. $value = acf_get_valid_terms($value, $field['taxonomy']); 
  82.  
  83.  
  84. // load/save 
  85. if( $field['load_save_terms'] ) { 
  86.  
  87. // bail early if no value 
  88. if( empty($value) ) { 
  89.  
  90. return $value; 
  91.  
  92.  
  93.  
  94. // get current ID's 
  95. $term_ids = wp_get_object_terms($post_id, $field['taxonomy'], array('fields' => 'ids', 'orderby' => 'none')); 
  96.  
  97.  
  98. // case 
  99. if( empty($term_ids) ) { 
  100.  
  101. // 1. no terms for this post 
  102. return null; 
  103.  
  104. } elseif( is_array($value) ) { 
  105.  
  106. // 2. remove metadata terms which are no longer for this post 
  107. $value = array_map('intval', $value); 
  108. $value = array_intersect( $value, $term_ids ); 
  109.  
  110. } elseif( !in_array($value, $term_ids)) { 
  111.  
  112. // 3. term is no longer for this post 
  113. return null; 
  114.  
  115.  
  116.  
  117.  
  118. // return 
  119. return $value; 
  120.  
  121.  
  122. /** 
  123. * update_value() 
  124. * This filter is appied to the $value before it is updated in the db 
  125. * @type filter 
  126. * @since 3.6 
  127. * @date 23/01/13 
  128. * @param $value - the value which will be saved in the database 
  129. * @param $field - the field array holding all the field options 
  130. * @param $post_id - the $post_id of which the value will be saved 
  131. * @return $value - the modified value 
  132. */ 
  133.  
  134. function update_value( $value, $post_id, $field ) { 
  135.  
  136. // vars 
  137. if( is_array($value) ) { 
  138.  
  139. $value = array_filter($value); 
  140.  
  141.  
  142.  
  143. // load_save_terms 
  144. if( $field['load_save_terms'] ) { 
  145.  
  146. // vars 
  147. $taxonomy = $field['taxonomy']; 
  148.  
  149.  
  150. // force value to array 
  151. $term_ids = acf_force_type_array( $value ); 
  152.  
  153.  
  154. // convert to int 
  155. $term_ids = array_map('intval', $term_ids); 
  156.  
  157.  
  158. // bypass $this->set_terms if called directly from update_field 
  159. if( !did_action('acf/save_post') ) { 
  160.  
  161. wp_set_object_terms( $post_id, $term_ids, $taxonomy, false ); 
  162.  
  163. return $value; 
  164.  
  165.  
  166.  
  167. // initialize 
  168. if( empty($this->set_terms) ) { 
  169.  
  170. // create holder 
  171. $this->set_terms = array(); 
  172.  
  173.  
  174. // add action 
  175. add_action('acf/save_post', array($this, 'set_terms'), 15, 1); 
  176.  
  177.  
  178.  
  179. // append 
  180. if( empty($this->set_terms[ $taxonomy ]) ) { 
  181.  
  182. $this->set_terms[ $taxonomy ] = array(); 
  183.  
  184.  
  185. $this->set_terms[ $taxonomy ] = array_merge($this->set_terms[ $taxonomy ], $term_ids); 
  186.  
  187.  
  188.  
  189. // return 
  190. return $value; 
  191.  
  192.  
  193.  
  194. /** 
  195. * set_terms 
  196. * description 
  197. * @type function 
  198. * @date 26/11/2014 
  199. * @since 5.0.9 
  200. * @param $post_id (int) 
  201. * @return $post_id (int) 
  202. */ 
  203.  
  204. function set_terms( $post_id ) { 
  205.  
  206. // bail ealry if no terms 
  207. if( empty($this->set_terms) ) { 
  208.  
  209. return; 
  210.  
  211.  
  212.  
  213. // loop over terms 
  214. foreach( $this->set_terms as $taxonomy => $term_ids ) { 
  215.  
  216. wp_set_object_terms( $post_id, $term_ids, $taxonomy, false ); 
  217.  
  218.  
  219.  
  220. // reset array ( WP saves twice ) 
  221. $this->set_terms = array(); 
  222.  
  223.  
  224.  
  225. /** 
  226. * format_value_for_api() 
  227. * This filter is appied to the $value after it is loaded from the db and before it is passed back to the api functions such as the_field 
  228. * @type filter 
  229. * @since 3.6 
  230. * @date 23/01/13 
  231. * @param $value - the value which was loaded from the database 
  232. * @param $post_id - the $post_id from which the value was loaded 
  233. * @param $field - the field array holding all the field options 
  234. * @return $value - the modified value 
  235. */ 
  236.  
  237. function format_value_for_api( $value, $post_id, $field ) { 
  238.  
  239. // bail early if no value 
  240. if( empty($value) ) { 
  241.  
  242. return $value; 
  243.  
  244.  
  245.  
  246. // force value to array 
  247. $value = acf_force_type_array( $value ); 
  248.  
  249.  
  250. // convert values to int 
  251. $value = array_map('intval', $value); 
  252.  
  253.  
  254. // load posts if needed 
  255. if( $field['return_format'] == 'object' ) { 
  256.  
  257.  
  258. // get posts 
  259. $value = $this->get_terms( $value, $field["taxonomy"] ); 
  260.  
  261.  
  262.  
  263. // convert back from array if neccessary 
  264. if( $field['field_type'] == 'select' || $field['field_type'] == 'radio' ) { 
  265.  
  266. $value = array_shift($value); 
  267.  
  268.  
  269.  
  270. // return 
  271. return $value; 
  272.  
  273.  
  274. /** 
  275. * create_field() 
  276. * Create the HTML interface for your field 
  277. * @type action 
  278. * @since 3.6 
  279. * @date 23/01/13 
  280. * @param $field - an array holding all the field's data 
  281. */ 
  282.  
  283. function create_field( $field ) 
  284. // vars 
  285. $single_name = $field['name']; 
  286.  
  287.  
  288. // multi select? 
  289. if( $field['field_type'] == 'multi_select' ) 
  290. $field['multiple'] = 1; 
  291. $field['field_type'] = 'select'; 
  292. $field['name'] .= '[]'; 
  293. elseif( $field['field_type'] == 'checkbox' ) 
  294. $field['name'] .= '[]'; 
  295.  
  296. // value must be array! 
  297. if( !is_array($field['value']) ) 
  298. $field['value'] = array( $field['value'] ); 
  299.  
  300.  
  301. // vars 
  302. $args = array( 
  303. 'taxonomy' => $field['taxonomy'],  
  304. 'hide_empty' => false,  
  305. 'style' => 'none',  
  306. 'walker' => new acf_taxonomy_field_walker( $field ),  
  307. ); 
  308.  
  309. $args = apply_filters('acf/fields/taxonomy/wp_list_categories', $args, $field ); 
  310.  
  311. ?> 
  312. <div class="acf-taxonomy-field" data-load_save="<?php echo $field['load_save_terms']; ?>"> 
  313. <input type="hidden" name="<?php echo $single_name; ?>" value="" /> 
  314.  
  315. <?php if( $field['field_type'] == 'select' ): ?> 
  316.  
  317. <select id="<?php echo $field['id']; ?>" name="<?php echo $field['name']; ?>" <?php if( $field['multiple'] ): ?>multiple="multiple" size="5"<?php endif; ?>> 
  318. <?php if( $field['allow_null'] ): ?> 
  319. <option value=""><?php _e("None", 'acf'); ?></option> 
  320. <?php endif; ?> 
  321.  
  322. <?php else: ?> 
  323. <div class="categorychecklist-holder"> 
  324. <ul class="acf-checkbox-list"> 
  325. <?php if( $field['allow_null'] ): ?> 
  326. <li> 
  327. <label class="selectit"> 
  328. <input type="<?php echo $field['field_type']; ?>" name="<?php echo $field['name']; ?>" value="" /> <?php _e("None", 'acf'); ?> 
  329. </label> 
  330. </li> 
  331. <?php endif; ?> 
  332.  
  333. <?php endif; ?> 
  334.  
  335. <?php wp_list_categories( $args ); ?> 
  336.  
  337. <?php if( $field['field_type'] == 'select' ): ?> 
  338.  
  339. </select> 
  340.  
  341. <?php else: ?> 
  342.  
  343. </ul> 
  344. </div> 
  345.  
  346. <?php endif; ?> 
  347.  
  348. </div> 
  349. <?php 
  350.  
  351.  
  352.  
  353. /** 
  354. * create_options() 
  355. * Create extra options for your field. This is rendered when editing a field. 
  356. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  357. * @type action 
  358. * @since 3.6 
  359. * @date 23/01/13 
  360. * @param $field - an array holding all the field's data 
  361. */ 
  362.  
  363. function create_options( $field ) 
  364. // vars 
  365. $key = $field['name']; 
  366.  
  367. ?> 
  368. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  369. <td class="label"> 
  370. <label><?php _e("Taxonomy", 'acf'); ?></label> 
  371. </td> 
  372. <td> 
  373. <?php 
  374.  
  375. // vars 
  376. $choices = array(); 
  377. $taxonomies = get_taxonomies( array(), 'objects' ); 
  378. $ignore = array( 'post_format', 'nav_menu', 'link_category' ); 
  379.  
  380.  
  381. foreach( $taxonomies as $taxonomy ) 
  382. if( in_array($taxonomy->name, $ignore) ) 
  383. continue; 
  384.  
  385. $choices[ $taxonomy->name ] = $taxonomy->name; 
  386.  
  387.  
  388. do_action('acf/create_field', array( 
  389. 'type' => 'select',  
  390. 'name' => 'fields['.$key.'][taxonomy]',  
  391. 'value' => $field['taxonomy'],  
  392. 'choices' => $choices,  
  393. )); 
  394.  
  395. ?> 
  396. </td> 
  397. </tr> 
  398. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  399. <td class="label"> 
  400. <label><?php _e("Field Type", 'acf'); ?></label> 
  401. </td> 
  402. <td> 
  403. <?php  
  404. do_action('acf/create_field', array( 
  405. 'type' => 'select',  
  406. 'name' => 'fields['.$key.'][field_type]',  
  407. 'value' => $field['field_type'],  
  408. 'optgroup' => true,  
  409. 'choices' => array( 
  410. __("Multiple Values", 'acf') => array( 
  411. 'checkbox' => __('Checkbox', 'acf'),  
  412. 'multi_select' => __('Multi Select', 'acf') 
  413. ),  
  414. __("Single Value", 'acf') => array( 
  415. 'radio' => __('Radio Buttons', 'acf'),  
  416. 'select' => __('Select', 'acf') 
  417. )); 
  418. ?> 
  419. </td> 
  420. </tr> 
  421. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  422. <td class="label"> 
  423. <label><?php _e("Allow Null?", 'acf'); ?></label> 
  424. </td> 
  425. <td> 
  426. <?php  
  427. do_action('acf/create_field', array( 
  428. 'type' => 'radio',  
  429. 'name' => 'fields['.$key.'][allow_null]',  
  430. 'value' => $field['allow_null'],  
  431. 'choices' => array( 
  432. 1 => __("Yes", 'acf'),  
  433. 0 => __("No", 'acf'),  
  434. ),  
  435. 'layout' => 'horizontal',  
  436. )); 
  437. ?> 
  438. </td> 
  439. </tr> 
  440. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  441. <td class="label"> 
  442. <label><?php _e("Load & Save Terms to Post", 'acf'); ?></label> 
  443. </td> 
  444. <td> 
  445. <?php  
  446. do_action('acf/create_field', array( 
  447. 'type' => 'true_false',  
  448. 'name' => 'fields['.$key.'][load_save_terms]',  
  449. 'value' => $field['load_save_terms'],  
  450. 'message' => __("Load value based on the post's terms and update the post's terms on save", 'acf') 
  451. )); 
  452. ?> 
  453. </td> 
  454. </tr> 
  455. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  456. <td class="label"> 
  457. <label><?php _e("Return Value", 'acf'); ?></label> 
  458. </td> 
  459. <td> 
  460. <?php 
  461. do_action('acf/create_field', array( 
  462. 'type' => 'radio',  
  463. 'name' => 'fields['.$key.'][return_format]',  
  464. 'value' => $field['return_format'],  
  465. 'layout' => 'horizontal',  
  466. 'choices' => array( 
  467. 'object' => __("Term Object", 'acf'),  
  468. 'id' => __("Term ID", 'acf') 
  469. )); 
  470. ?> 
  471. </td> 
  472. </tr> 
  473. <?php 
  474.  
  475.  
  476.