Support

Account

Home Forums General Issues Share Field Values Across Pages

Solving

Share Field Values Across Pages

  • Is it possible to share field group values across posts/pages?

    For instance, with a competition schedule:

    There are Event Pages, with a line-up of performers. Each performance has a time, performer, and ranking (added later).

    Event Z: 2:15 – Performer A – ***** / 2:30 – Performer B – ***

    There are also Performer pages, which lists which events they go to. Performers may attend multiple events. Rankings get added to performer pages after the event.

    Performer A: Event Y: ** / Event Z: *****

    Is it possible to create relationships between fields across posts types/pages, to relate the data so once I add information to Event Z, I don’t have to go back and add it to the associated Performers?

  • // current post
    global $post;
    
    // args for query
    $query_args = array(
    	'post_type' => 'performers', // performers post_type (or use WP_User_query  for users)
    	'meta_query' => array(
    		array(
    			// performers match field slug
    			'key' => 'event_id', // field slug acf (use post object field and return id)
    			'value' => $post->ID // current post id
    		)
    	)
    );
    
    // set wp  query
    $query = new WP_Query( $query_args );
    
    // query have posts
    if( $query->have_posts() ):
    
    	// loop posts (performers)
    	while( $query->have_posts() ): $query->the_post();
    
    		// update fields you like
    		// field_slug, field_value, current perfomers id
    		update_field( 'YOUR_FIELD_SLUG', 'YOU_FIELD_VALUE', get_the_ID() );
    
    	// end loop
    	endwhile;
    
    	// reset query
    	wp_reset_postdata();
    
    // end
    endif;

    I do not know if I understood you correctly, but I’m thinking (depending on your own needs) that you should reach your goal.

    You can also put that into a function and then use it the way you need it everywhere.

    If you want to do this via / wp-admin, you should look at acf/save_post and combine it.

    usefull links:
    WP_Query
    WP_User_Query
    acf/save_post

  • Kevin,

    Thanks for your response. I’m not sure I fully understand the above, but my design and needs have changed, and I’m getting closer to understanding what I need… and maybe you can help again?

    This code will be on a Band Page, calling to a Competition Page. Each have repeaters… band repeats competition list, competition repeats band and scores. I want to call a specific band’s scores from the competition page. I’m not sure how to specify the repeater row.

    <?php
    $post_object = get_sub_field(‘competition’);
    if( $post_object): $post = $post_object;
    setup_postdata( $post ); ?>

    “><?php the_title(); ?><p>

    <?php
    if( have_rows(‘competition_schedule’) ):
    while ( have_rows(‘competition_schedule’) ) : the_row();
    the_sub_field(‘band_score’);

    endwhile;

    else :

    // no rows found

    endif; ?></p>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>

    But right now, each band is pulling the same score.

    https://www.michiganmarching.com/bund/new-bund
    https://www.michiganmarching.com/bund/three-bund

    While I haven’t touched a template yet for the competitions in this regard, “State fight” does have both New Bund and Three Bund as in there, within the repeater, with different scores.

  • You can simply use this:

    // your repeater
    $items = get_field('competition_schedule');
    
    if( $items ) {
      foreach( $items as $item ) {
        echo $item['band_score'];
      }
    }
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.