Support

Account

Home Forums Add-ons Repeater Field Using get_post_meta as Fallback on Repeaters

Solved

Using get_post_meta as Fallback on Repeaters

  • Looking at implementing a fall back using get_post_meta, I recently did a home page which was all ACF get_field so I redid it with the fallbacks which work great, issue is I am also using a repeater field which extracts the last row of another page and displays it on the home page – I tried the fallback with get_post_meta but nothing is displaying, I think my issue is with the subfields – any pointers –

    <?php
    
    if( function_exists('get_field') ){
    
    //Display Latest Testimonial Row with ACF;
    echo '<div class="info">';
    $rows = get_field( 'testimonials', 348 ); // get all the rows
    $end_row = end($rows); // get the end row
    $end_testimonial_content = $end_row['testimonial' ]; // get the sub field value 
    $end_testimonial_header = $end_row['testimonial_header' ]; // get the sub field value 
    $last =  '<blockquote>' . $end_testimonial_content . '</blockquote><p>' . $end_testimonial_header . '</p>';
    echo $last;
    echo '</div>';
    }
    
    else {
    
    //Fallback without ACF;
    echo '<div class="info">';
    $rows =  get_post_meta( get_the_ID(348), 'testimonials', false ); // get all the rows
    $end_row = end($rows); // get the end row
    $end_testimonial_content = $end_row['testimonial' ]; // get the sub field value 
    $end_testimonial_header = $end_row['testimonial_header' ]; // get the sub field value 
    $last =  '<blockquote>' . $end_testimonial_content . '</blockquote><p>' . $end_testimonial_header . '</p>';
    echo $last; 
    echo '</div>';
    }
  • One thing i could say is, both the repeater and flexible content fields save their data in this format:
    $ParentName_$RowNumber_$ChildName

    the values of testimonial for the first and second row are saved into this 2 DB-Fields.

    'testimonials_0_testimonial' //for first row
    'testimonials_1_testimonial' //for second row

    maybe that help.

    else: debug $rows => that means: look what inside $rows is.

    echo '<pre>';
    print_r( $rows);
    echo '</pre>';

    probably it is not array like you expect (because each subfield has it own db-row)

  • ok thanks – so I can get a specific row output like so…

    <?php 	
    	$rows =  get_post_meta( 348, 'testimonials', false ); // get all the rows
    	$end_testimonial_content = get_post_meta( 348, 'testimonials_4_testimonial', true );// get the sub field value 
    	$end_testimonial_header = get_post_meta( 348, 'testimonials_4_testimonial_header', true ); // get the sub field value 
    	$last =  '<blockquote>' . $end_testimonial_content . '</blockquote><p>' . $end_testimonial_header . '</p>';
    	echo $last; 
    
    	?>

    I also tried with end function but couldn’t get it to work, but as a fallback at least I have something on the page.

  • probably your end function return 5 (5 rows)
    but row 5 use _4_(beause rows start with _0_)

    you dont need this line to get subfields:
    $rows = get_post_meta( 348, 'testimonials', false ); // get all the rows
    but what is inside $rows?
    if it is the row-count, or you could get it from it.
    than use $rows--; after and you have the number you can use for the last row

  • okay result i get from

    echo '<pre>';
    print_r( $rows);
    echo '</pre>';

    is

    Array
    (
        [0] => 8
    )

    Which is the 8 repeater rows current in the array.

    how do i use $rows--; ?
    and/or how would i always echo the last row after more rows where added?

  • because $rows always have the count of rows you could use:

    $rows = get_post_meta( 348, 'testimonials', false ); // get number of rows (8 but in a array)
    $row_count = $rows[0]; //get row number out of array (8)
    $row_count--; // because first row start with 0 not 1 subtract one from row number (8-1 =7)
    //now you could use $row_count to get last row (7)
    echo 'testimonials_'.$row_count.'_testimonial'; //you need to replace echo by a variable and use that

    Hint:
    $rows--; is short form of $rows = $rows-1;
    because $rows and better $row_count always have the correct row number you can add/remove rows without problems

  • tried to this using

    $rows = get_post_meta( 348, 'testimonials', false ); // get number of rows (8 but in a array)
    $row_count = $rows[0]; //get row number out of array (8)
    echo $row_count--; // because first row start with 0 not 1 subtract one from row number (8-1 =7)
    //now you could use $row_count to get last row (7)
    $lastrow =  'testimonials_'.$row_count.'_testimonial'; //you need to replace echo by a variable and use that
    
    echo $lastrow;

    What i get front end is
    8testimonials_7_testimonial

  • that is good. you see that it works 😉
    the 8 is because you echo $row_count--;
    and testimonials_7_testimonial is the field name for the last row.

    now use something like that:

    $rows = get_post_meta( 348, 'testimonials', false ); // get number of rows, is inside a array
    $row_count = $rows[0]; //get row number out of array
    $row_count--; // because first row start with 0 not 1 subtract one from row number
    $lastrow_content =  'testimonials_'.$row_count.'_testimonial'; //get last row field
    $lastrow_head = 'testimonials_'.$row_count.'_testimonial_header'; //get last row field
    $end_testimonial_content = get_post_meta( 348, $lastrow_content, true );// get the last row value
    $end_testimonial_header = get_post_meta( 348, $lastrow_head, true ); // get the last row value 
    $last =  '<blockquote>' . $end_testimonial_content . '</blockquote><p>' . $end_testimonial_header . '</p>';
    echo $last;
  • Wow you do know your stuff! – Many thanks that works perfectly, the only thing that i don’t grasp 100% is
    $row_count--;

    I do understand by its use we are moving from zero based index to get the last proper row.

    You said earlier that:
    $rows--; is short form of $rows = $rows-1;

    So in the code that works, instead of using $row_count--; i tried $row_count-1;
    but that outputs nothing, I was wondering for my own curiousity how to use arithmetic operator on the row count so for instance I could output the 2nd last one.

  • $row_count-1; would not work because you dont save result back to a variable.
    $row_count_last = $row_count-1; should work 😉
    (you can use same variable like previous post, or a new one like this post)

    for second last you could use different ways.
    it depends on things like: do you use last row anyway.

    $row_count--; // because first row start with 0 not 1 subtract one from row number
    $lastrow_content =  'testimonials_'.$row_count.'_testimonial'; //get last row field
    $lastrow_head = 'testimonials_'.$row_count.'_testimonial_header'; //get last row field
    $row_count--; // because we already subtract 1, this is like subtract 2 from start value
    $2lastrow_content =  'testimonials_'.$row_count.'_testimonial'; //get 2last row field
    $2lastrow_head = 'testimonials_'.$row_count.'_testimonial_header'; //get 2last row field

    above only works when variable for last row is filled before.
    (it use $row_count--;)
    below works independent of ordering, last could be before or after 2last
    (this dont use $row_count--; !)

    $row_count_2last = $row_count-2;
    $2lastrow_content =  'testimonials_'.$row_count_2last.'_testimonial'; //get 2last row field
    $2lastrow_head = 'testimonials_'.$row_count_2last.'_testimonial_header'; //get 2last row field
    $row_count_last = $row_count-1; // because first row start with 0 not 1 subtract one from row number
    $lastrow_content =  'testimonials_'.$row_count_last.'_testimonial'; //get last row field
    $lastrow_head = 'testimonials_'.$row_count_last.'_testimonial_header'; //get last row field

    or you could do it a other way 😉 (fill your own array and echo that for example)

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

You must be logged in to reply to this topic.