have_rows

The WordPress Core have rows function.

Description

have_rows( (string) $field_name, (bool) $post_id = false ); 

This function will instantiate a global variable containing the rows of a repeater or flexible content field, afterwhich, it will determine if another row exists to loop through

Parameters (2)

0. $field_name (string)
The field name.
1. $post_id — Optional. (bool) => false
The post id.

Usage

  1. if ( !function_exists( 'have_rows' ) ) { 
  2. require_once '/core/api.php'; 
  3.  
  4. // The field name. 
  5. $field_name = ''; 
  6.  
  7. // The post id. 
  8. $post_id = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = have_rows($field_name, $post_id); 
  12.  

Defined (1)

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

/core/api.php  
  1. function have_rows( $field_name, $post_id = false ) { 
  2.  
  3. // vars 
  4. $depth = 0; 
  5. $row = array(); 
  6. $new_parent_loop = false; 
  7. $new_child_loop = false; 
  8.  
  9.  
  10. // reference 
  11. $_post_id = $post_id; 
  12.  
  13.  
  14. // filter post_id 
  15. $post_id = apply_filters('acf/get_post_id', $post_id ); 
  16.  
  17.  
  18. // empty? 
  19. if( empty($GLOBALS['acf_field']) ) 
  20. // reset 
  21. reset_rows( true ); 
  22.  
  23.  
  24. // create a new loop 
  25. $new_parent_loop = true; 
  26. else 
  27. // vars 
  28. $row = end( $GLOBALS['acf_field'] ); 
  29. $prev = prev( $GLOBALS['acf_field'] ); 
  30.  
  31.  
  32. // If post_id has changed, this is most likely an archive loop 
  33. if( $post_id != $row['post_id'] ) 
  34. if( $prev && $prev['post_id'] == $post_id ) 
  35. // case: Change in $post_id was due to a nested loop ending 
  36. // action: move up one level through the loops 
  37. reset_rows(); 
  38. elseif( empty($_post_id) && isset($row['value'][ $row['i'] ][ $field_name ]) ) 
  39. // case: Change in $post_id was due to this being a nested loop and not specifying the $post_id 
  40. // action: move down one level into a new loop 
  41. $new_child_loop = true; 
  42. else 
  43. // case: Chang in $post_id is the most obvious, used in an WP_Query loop with multiple $post objects 
  44. // action: leave this current loop alone and create a new parent loop 
  45. $new_parent_loop = true; 
  46. elseif( $field_name != $row['name'] ) 
  47. if( $prev && $prev['name'] == $field_name && $prev['post_id'] == $post_id ) 
  48. // case: Change in $field_name was due to a nested loop ending 
  49. // action: move up one level through the loops 
  50. reset_rows(); 
  51. elseif( isset($row['value'][ $row['i'] ][ $field_name ]) ) 
  52. // case: Change in $field_name was due to this being a nested loop 
  53. // action: move down one level into a new loop 
  54. $new_child_loop = true; 
  55.  
  56. else 
  57. // case: Chang in $field_name is the most obvious, this is a new loop for a different field within the $post 
  58. // action: leave this current loop alone and create a new parent loop 
  59. $new_parent_loop = true; 
  60.  
  61.  
  62.  
  63.  
  64. if( $new_parent_loop ) 
  65. // vars 
  66. $f = get_field_object( $field_name, $post_id ); 
  67. $v = $f['value']; 
  68. unset( $f['value'] ); 
  69.  
  70.  
  71. // add row 
  72. $GLOBALS['acf_field'][] = array( 
  73. 'name' => $field_name,  
  74. 'value' => $v,  
  75. 'field' => $f,  
  76. 'i' => -1,  
  77. 'post_id' => $post_id,  
  78. ); 
  79.  
  80. elseif( $new_child_loop ) 
  81. // vars 
  82. $f = acf_get_child_field_from_parent_field( $field_name, $row['field'] ); 
  83. $v = $row['value'][ $row['i'] ][ $field_name ]; 
  84.  
  85. $GLOBALS['acf_field'][] = array( 
  86. 'name' => $field_name,  
  87. 'value' => $v,  
  88. 'field' => $f,  
  89. 'i' => -1,  
  90. 'post_id' => $post_id,  
  91. ); 
  92.  
  93. }  
  94.  
  95.  
  96. // update vars 
  97. $row = end( $GLOBALS['acf_field'] ); 
  98.  
  99.  
  100. if( is_array($row['value']) && array_key_exists( $row['i']+1, $row['value'] ) ) 
  101. // next row exists 
  102. return true; 
  103.  
  104.  
  105. // no next row! 
  106. reset_rows(); 
  107.  
  108.  
  109. // return 
  110. return false; 
  111.