Support

Account

Home Forums ACF PRO ACF – getting fields in functions.php

Solved

ACF – getting fields in functions.php

  • Hi

    Im trying to get a textfield from another post, into current post.

    I’ve got a repeater field, with a post-object field(that returns postID) – this ID links to another post, where I got a text field (get_field(‘tekst’)).

    get_field(‘tekst’) should then be copied to a text field on the current page (The field called “Meta Beskrivelse” – see attachment)

    I got this code in functions.php – but no luck. Any ideas?

    function my_acf_load_value( $value, $post_id, $field ) {
      global $post;
      $parentartikelID = $post->ID;
    
      $args = array( 'page_id' => $parentartikelID );
      $loop = new WP_Query($args);
    
      while ( have_posts() ) : the_post();
    
        if( have_rows('artikel') ):
            while ( have_rows('artikel') ) : the_row();
              $artikelID = get_field('artikel_navn');
    
              $argssub = array('pagename' => $artikelID );
              $loop = new WP_Query( $argssub );
    
              while ( $loop->have_posts() ) : $loop->the_post();
                $value = get_field('tekst');
              endwhile;
    
            endwhile;
          endif;
        endwhile;
    
    return $value;
    }
    add_filter('acf/update_value/name=meta_beskrivelse', 'my_acf_load_value', 10, 3);
  • 
    function my_acf_load_value($value, $post_id, $field) {
      // current post id is passed, no need to figure it out
      if (have_rows('artikel', $post_id)) {
        // set up an array to hold values
        // this is becuase a repeater field could have many
        // and will make it easier to concatinate them
        $values = array();
        while (have_rows('artikel', $post_id)) {
          // get value of sub field without formatting
          // so that it will be the ID of the post object
          $post_object_id = get_sub_field('artikel_navn', false);
          // get the field value from the related post object
          // and add to the array
          $values[] = get_field('tekst', $post_object_id);
        } // end while have rows
        // after the loop, implode the values
        $value = implode(', ', $values);
      } // end if have rows
    
      return $value;
    }
    add_filter('acf/update_value/name=meta_beskrivelse', 'my_acf_load_value', 10, 3);
    
  • Hi John

    Thank you so much for your time and effort.

    I’m getting a time-out. I think it is pulling to many posts.
    Thought about adding a query (the page is a custom post type), but now i’m getting an error message.

    Warning: htmlspecialchars() expects parameter 1 to be string, object given

    function my_acf_load_value($value, $post_id, $field) {
    $query = new WP_Query( array( 'post_type' => 'artikler', 'posts_per_page' => 1, ) );
    return $query;
    
      if ($query->have_rows('artikel', $post_id)) {
        while ($query->have_rows('artikel', $post_id)) {
          $post_object_id = get_sub_field('artikel_navn', false);
          $values = get_field('tekst', $post_object_id);
        }
        $value = implode(', ', $values);
      }
      return $value;
    }
    add_filter('acf/update_value/name=meta_beskrivelse', 'my_acf_load_value', 10, 3);
  • Well, that’s because I forgot something important

    
    while ($query->have_rows('artikel', $post_id)) {
      the_row(); // I forgot this
    
  • Thank you so much, John – you are a star! It works πŸ™‚

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

You must be logged in to reply to this topic.