Search code examples
wordpressloopstagsrepeater

How to turn repeater subfield into tag


I have a loop which goes through a repeater to output different values of a date&time subfield in the format of day ('d'). I would like to know how can I turn each different value of $dia into a tag, in case there are multiple values for it (the problem is that what I have done so far outputs only the last value of $dia).

This is what I have so far:


if( have_rows('quando', $post_id) ):
 while( have_rows('quando', $post_id) ): the_row();

   $dataCrua = get_sub_field('dia_e_hora', $post_id);
   $data = DateTime::createFromFormat("Y-m-d H:i:s", $dataCrua);
     if ( is_object($data) ) {
       $dia = $data->format('d');
     }

   $tags = $dia;
   wp_set_post_tags( $post_id, $tags);

 endwhile;
endif;
 ?>

Solution

  • Solved it!

        $tags = array();
        if( have_rows('quando', $post_id) ):
          while( have_rows('quando', $post_id) ): the_row();
    
        $dataCrua = get_sub_field('dia_e_hora', $post_id);
                $data = DateTime::createFromFormat("Y-m-d H:i:s", $dataCrua);
          if ( is_object($data) ) {
                  $dia = $data->format('d');
                }
    
                $tags[] = $dia;
                wp_set_post_tags( $post_id, $tags, false);
    
            endwhile;
        endif;
         ?>