Support

Account

Home Forums ACF PRO have_rows() Issue

Solved

have_rows() Issue

  • I have a custom loop in an archive template. I’m using the current version of ACF Pro.

    I’m using the genesis_post_title_text filter to modify the title, which is dependent on the current post’s flexible content field. This works fine, every time.

    I’m also using the genesis_entry_content action to add custom content, which is contained in the current post’s same flexible content field.

    With both the filter and action active, have_rows() will return FALSE in the action, but work as expected in the filter.

    (Incidentally, get_field(‘flex_field_name’) returns the expected structure when have_rows() is returning FALSE.)

    With the filter disabled, the action’s have_rows() will behave as expected.

    Is this a bug?

  • I’m not sure I completely understand, so I’m guessing here.

    You have 2 functions (filter or action) and you’re looping through the same repeater in both of these funtions, so 2 loops on the same repeater/flex field.

  • @hube2 – Correct.

    The title filter (called first) is using have_rows() and the content action (called second) is using have_rows() on the same post.

    I’ve also tried have_rows(‘field’,$post->ID) in both functions. The same behavior occurs — the first function works and the second one that uses have_rows() does not.

  • Try using reset_rows()

    
    if (have_rows('field_name')) {
      while (have_rows('field_name')) {
        // code
      }
      reset_rows();
    }
    
  • @hube2 – Thanks, but no go. have_rows() still returns FALSE in the second function.

    // Check for alternative title
    add_filter( 'genesis_post_title_text', 'bk_process_title' );
    function bk_process_title($title) {
      if(have_rows('flex_content')) {
        while(have_rows('flex_content')) {
          the_row();
          if(get_row_layout() == 'sales') {
            $acf_field = get_sub_field('title');
            return $acf_field ? $acf_field : $title;
          }
        }
        reset_rows();
      } else {
        return $title;
      }
    }
    // Check for alternative listing content
    remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
    add_action( 'genesis_entry_content', 'bk_process_content', 11 );
    function bk_process_content() {
      if(have_rows('flex_content')) {
        while(have_rows('flex_content')) {
          the_row();
          if(get_row_layout() == 'sales') {
            $acf_field = get_sub_field('deck');
            if($acf_field) {
              echo $acf_field;
            } 
          }
        }
        reset_rows();
      } else {
        the_excerpt();
      }
    }
  • I think it might be because you’re returning a value before reset_rows() is called. Try the reset before the return.

  • @hube2 – Thanks. My bad there on the logic — was a quick check. I’ve refactored and you are indeed correct: reset_rows() is the answer.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘have_rows() Issue’ is closed to new replies.