acf_field_post_object

The Advanced Custom Fields acf field post object class.

Defined (1)

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

/core/fields/post_object.php  
  1. class acf_field_post_object 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 = 'post_object'; 
  12. $this->label = __("Post Object", 'acf'); 
  13. $this->category = __("Relational", 'acf'); 
  14. $this->defaults = array( 
  15. 'post_type' => array('all'),  
  16. 'taxonomy' => array('all'),  
  17. 'multiple' => 0,  
  18. 'allow_null' => 0,  
  19. ); 
  20.  
  21.  
  22. // do not delete! 
  23. parent::__construct(); 
  24.  
  25.  
  26.  
  27. /** 
  28. * load_field() 
  29. *  
  30. * This filter is appied to the $field after it is loaded from the database 
  31. *  
  32. * @type filter 
  33. * @since 3.6 
  34. * @date 23/01/13 
  35. *  
  36. * @param $field - the field array holding all the field options 
  37. *  
  38. * @return $field - the field array holding all the field options 
  39. */ 
  40.  
  41. function load_field( $field ) 
  42. // validate post_type 
  43. if( !$field['post_type'] || !is_array($field['post_type']) || in_array('', $field['post_type']) ) 
  44. $field['post_type'] = array( 'all' ); 
  45.  
  46.  
  47. // validate taxonomy 
  48. if( !$field['taxonomy'] || !is_array($field['taxonomy']) || in_array('', $field['taxonomy']) ) 
  49. $field['taxonomy'] = array( 'all' ); 
  50.  
  51.  
  52. // return 
  53. return $field; 
  54.  
  55.  
  56. /** 
  57. * create_field() 
  58. * Create the HTML interface for your field 
  59. * @param $field - an array holding all the field's data 
  60. * @type action 
  61. * @since 3.6 
  62. * @date 23/01/13 
  63. */ 
  64.  
  65. function create_field( $field ) 
  66. // global 
  67. global $post; 
  68.  
  69.  
  70. // vars 
  71. $args = array( 
  72. 'numberposts' => -1,  
  73. 'post_type' => null,  
  74. 'orderby' => 'title',  
  75. 'order' => 'ASC',  
  76. 'post_status' => array('publish', 'private', 'draft', 'inherit', 'future'),  
  77. 'suppress_filters' => false,  
  78. ); 
  79.  
  80.  
  81. // load all post types by default 
  82. if( in_array('all', $field['post_type']) ) 
  83. $field['post_type'] = apply_filters('acf/get_post_types', array()); 
  84.  
  85.  
  86. // create tax queries 
  87. if( ! in_array('all', $field['taxonomy']) ) 
  88. // vars 
  89. $taxonomies = array(); 
  90. $args['tax_query'] = array(); 
  91.  
  92. foreach( $field['taxonomy'] as $v ) 
  93.  
  94. // find term (find taxonomy!) 
  95. // $term = array( 0 => $taxonomy, 1 => $term_id ) 
  96. $term = explode(':', $v);  
  97.  
  98.  
  99. // validate 
  100. if( !is_array($term) || !isset($term[1]) ) 
  101. continue; 
  102.  
  103.  
  104. // add to tax array 
  105. $taxonomies[ $term[0] ][] = $term[1]; 
  106.  
  107.  
  108.  
  109. // now create the tax queries 
  110. foreach( $taxonomies as $k => $v ) 
  111. $args['tax_query'][] = array( 
  112. 'taxonomy' => $k,  
  113. 'field' => 'id',  
  114. 'terms' => $v,  
  115. ); 
  116.  
  117.  
  118. // Change Field into a select 
  119. $field['type'] = 'select'; 
  120. $field['choices'] = array(); 
  121.  
  122.  
  123. foreach( $field['post_type'] as $post_type ) 
  124. // set post_type 
  125. $args['post_type'] = $post_type; 
  126.  
  127.  
  128. // set order 
  129. $get_pages = false; 
  130. if( is_post_type_hierarchical($post_type) && !isset($args['tax_query']) ) 
  131. $args['sort_column'] = 'menu_order, post_title'; 
  132. $args['sort_order'] = 'ASC'; 
  133.  
  134. $get_pages = true; 
  135.  
  136.  
  137. // filters 
  138. $args = apply_filters('acf/fields/post_object/query', $args, $field, $post); 
  139. $args = apply_filters('acf/fields/post_object/query/name=' . $field['_name'], $args, $field, $post ); 
  140. $args = apply_filters('acf/fields/post_object/query/key=' . $field['key'], $args, $field, $post ); 
  141.  
  142.  
  143. if( $get_pages ) 
  144. $posts = get_pages( $args ); 
  145. else 
  146. $posts = get_posts( $args ); 
  147.  
  148.  
  149. if($posts) { 
  150.  
  151. foreach( $posts as $p ) { 
  152.  
  153. // title 
  154. $title = get_the_title( $p->ID ); 
  155.  
  156.  
  157. // empty 
  158. if( $title === '' ) { 
  159.  
  160. $title = __('(no title)', 'acf'); 
  161.  
  162.  
  163.  
  164. // ancestors 
  165. if( $p->post_type != 'attachment' ) { 
  166.  
  167. $ancestors = get_ancestors( $p->ID, $p->post_type ); 
  168.  
  169. $title = str_repeat('- ', count($ancestors)) . $title; 
  170.  
  171.  
  172.  
  173. // status 
  174. if( get_post_status( $p->ID ) != "publish" ) { 
  175.  
  176. $title .= ' (' . get_post_status( $p->ID ) . ')'; 
  177.  
  178.  
  179.  
  180. // WPML 
  181. if( defined('ICL_LANGUAGE_CODE') ) { 
  182.  
  183. $title .= ' (' . ICL_LANGUAGE_CODE . ')'; 
  184.  
  185.  
  186.  
  187. // filters 
  188. $title = apply_filters('acf/fields/post_object/result', $title, $p, $field, $post); 
  189. $title = apply_filters('acf/fields/post_object/result/name=' . $field['_name'] , $title, $p, $field, $post); 
  190. $title = apply_filters('acf/fields/post_object/result/key=' . $field['key'], $title, $p, $field, $post); 
  191.  
  192.  
  193. // add to choices 
  194. if( count($field['post_type']) == 1 ) 
  195. $field['choices'][ $p->ID ] = $title; 
  196. else 
  197. // group by post type 
  198. $post_type_object = get_post_type_object( $p->post_type ); 
  199. $post_type_name = $post_type_object->labels->name; 
  200.  
  201. $field['choices'][ $post_type_name ][ $p->ID ] = $title; 
  202.  
  203.  
  204. // foreach( $posts as $post ) 
  205. // if($posts) 
  206. // foreach( $field['post_type'] as $post_type ) 
  207.  
  208.  
  209. // create field 
  210. do_action('acf/create_field', $field ); 
  211.  
  212.  
  213. /** 
  214. * create_options() 
  215. * Create extra options for your field. This is rendered when editing a field. 
  216. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  217. * @type action 
  218. * @since 3.6 
  219. * @date 23/01/13 
  220. * @param $field - an array holding all the field's data 
  221. */ 
  222.  
  223. function create_options( $field ) 
  224. // vars 
  225. $key = $field['name']; 
  226.  
  227. ?> 
  228. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  229. <td class="label"> 
  230. <label for=""><?php _e("Post Type", 'acf'); ?></label> 
  231. </td> 
  232. <td> 
  233. <?php  
  234.  
  235. $choices = array( 
  236. 'all' => __("All", 'acf') 
  237. ); 
  238. $choices = apply_filters('acf/get_post_types', $choices); 
  239.  
  240.  
  241. do_action('acf/create_field', array( 
  242. 'type' => 'select',  
  243. 'name' => 'fields['.$key.'][post_type]',  
  244. 'value' => $field['post_type'],  
  245. 'choices' => $choices,  
  246. 'multiple' => 1,  
  247. )); 
  248.  
  249. ?> 
  250. </td> 
  251. </tr> 
  252. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  253. <td class="label"> 
  254. <label><?php _e("Filter from Taxonomy", 'acf'); ?></label> 
  255. </td> 
  256. <td> 
  257. <?php  
  258. $choices = array( 
  259. '' => array( 
  260. 'all' => __("All", 'acf') 
  261. ); 
  262. $simple_value = false; 
  263. $choices = apply_filters('acf/get_taxonomies_for_select', $choices, $simple_value); 
  264.  
  265. do_action('acf/create_field', array( 
  266. 'type' => 'select',  
  267. 'name' => 'fields['.$key.'][taxonomy]',  
  268. 'value' => $field['taxonomy'],  
  269. 'choices' => $choices,  
  270. 'multiple' => 1,  
  271. )); 
  272.  
  273. ?> 
  274. </td> 
  275. </tr> 
  276. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  277. <td class="label"> 
  278. <label><?php _e("Allow Null?", 'acf'); ?></label> 
  279. </td> 
  280. <td> 
  281. <?php 
  282.  
  283. do_action('acf/create_field', array( 
  284. 'type' => 'radio',  
  285. 'name' => 'fields['.$key.'][allow_null]',  
  286. 'value' => $field['allow_null'],  
  287. 'choices' => array( 
  288. 1 => __("Yes", 'acf'),  
  289. 0 => __("No", 'acf'),  
  290. ),  
  291. 'layout' => 'horizontal',  
  292. )); 
  293.  
  294. ?> 
  295. </td> 
  296. </tr> 
  297. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  298. <td class="label"> 
  299. <label><?php _e("Select multiple values?", 'acf'); ?></label> 
  300. </td> 
  301. <td> 
  302. <?php 
  303.  
  304. do_action('acf/create_field', array( 
  305. 'type' => 'radio',  
  306. 'name' => 'fields['.$key.'][multiple]',  
  307. 'value' => $field['multiple'],  
  308. 'choices' => array( 
  309. 1 => __("Yes", 'acf'),  
  310. 0 => __("No", 'acf'),  
  311. ),  
  312. 'layout' => 'horizontal',  
  313. )); 
  314.  
  315. ?> 
  316. </td> 
  317. </tr> 
  318. <?php 
  319.  
  320.  
  321.  
  322. /** 
  323. * format_value() 
  324. * This filter is appied to the $value after it is loaded from the db and before it is passed to the create_field action 
  325. * @type filter 
  326. * @since 3.6 
  327. * @date 23/01/13 
  328. * @param $value - the value which was loaded from the database 
  329. * @param $post_id - the $post_id from which the value was loaded 
  330. * @param $field - the field array holding all the field options 
  331. * @return $value - the modified value 
  332. */ 
  333.  
  334. function format_value( $value, $post_id, $field ) 
  335. // empty? 
  336. if( !empty($value) ) 
  337. // convert to integers 
  338. if( is_array($value) ) 
  339. $value = array_map('intval', $value); 
  340. else 
  341. $value = intval($value); 
  342.  
  343.  
  344. // return value 
  345. return $value;  
  346.  
  347.  
  348. /** 
  349. * format_value_for_api() 
  350. * 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 
  351. * @type filter 
  352. * @since 3.6 
  353. * @date 23/01/13 
  354. * @param $value - the value which was loaded from the database 
  355. * @param $post_id - the $post_id from which the value was loaded 
  356. * @param $field - the field array holding all the field options 
  357. * @return $value - the modified value 
  358. */ 
  359.  
  360. function format_value_for_api( $value, $post_id, $field ) 
  361. // no value? 
  362. if( !$value ) 
  363. return false; 
  364.  
  365.  
  366. // null? 
  367. if( $value == 'null' ) 
  368. return false; 
  369.  
  370.  
  371. // multiple / single 
  372. if( is_array($value) ) 
  373. // find posts (DISTINCT POSTS) 
  374. $posts = get_posts(array( 
  375. 'numberposts' => -1,  
  376. 'post__in' => $value,  
  377. 'post_type' => apply_filters('acf/get_post_types', array()),  
  378. 'post_status' => array('publish', 'private', 'draft', 'inherit', 'future'),  
  379. )); 
  380.  
  381.  
  382. $ordered_posts = array(); 
  383. foreach( $posts as $post ) 
  384. // create array to hold value data 
  385. $ordered_posts[ $post->ID ] = $post; 
  386.  
  387.  
  388. // override value array with attachments 
  389. foreach( $value as $k => $v) 
  390. // check that post exists (my have been trashed) 
  391. if( !isset($ordered_posts[ $v ]) ) 
  392. unset( $value[ $k ] ); 
  393. else 
  394. $value[ $k ] = $ordered_posts[ $v ]; 
  395.  
  396. else 
  397. $value = get_post($value); 
  398.  
  399.  
  400. // return the value 
  401. return $value; 
  402.  
  403.  
  404. /** 
  405. * update_value() 
  406. * This filter is appied to the $value before it is updated in the db 
  407. * @type filter 
  408. * @since 3.6 
  409. * @date 23/01/13 
  410. * @param $value - the value which will be saved in the database 
  411. * @param $post_id - the $post_id of which the value will be saved 
  412. * @param $field - the field array holding all the field options 
  413. * @return $value - the modified value 
  414. */ 
  415.  
  416. function update_value( $value, $post_id, $field ) 
  417. // validate 
  418. if( empty($value) ) 
  419. return $value; 
  420.  
  421.  
  422. if( is_object($value) && isset($value->ID) ) 
  423. // object 
  424. $value = $value->ID; 
  425.  
  426. elseif( is_array($value) ) 
  427. // array 
  428. foreach( $value as $k => $v ) { 
  429.  
  430. // object? 
  431. if( is_object($v) && isset($v->ID) ) 
  432. $value[ $k ] = $v->ID; 
  433.  
  434. // save value as strings, so we can clearly search for them in SQL LIKE statements 
  435. $value = array_map('strval', $value); 
  436.  
  437.  
  438. return $value; 
  439.