Support

Account

Home Forums General Issues Using get_field to set a variable with values from a user Reply To: Using get_field to set a variable with values from a user

  • Okay, I think we’re getting close. The code below (based on your helpful code above) kind of works but is only storing one lot of release leg ring data in the $value variable.

    <?php
    $args = array(
      'post_type' => 'wglocations',
      'post_per_page' => -1,
      'category_name' => 'wg-release',
      'author' => $current_user->ID,
    );
    $query = new WP_Query($args);
    foreach ($query->posts as $post) {
      $value = get_field('ring_numbers');
      $values = str_replace(' ', '', $ring_numbers);
      $ring_numbers = array_merge($ring_numbers, explode(',', $value));
    }
    
    echo '<p>$value: ' . $value . '</p>'; // Check what the $value variable is storing
    
    $args = array(
      'post_type' => 'wglocations',
      'posts_per_page' => -1,
      'category_name' => 'wg-finding',
      'meta_query' => array (
        array (
          'key' => 'leg_ring_number',
          'value' => $value, // Use the user's 'released' ring numbers
          'compare' => 'IN', // Compare them to any 'found' ring numbers
        )
      )
    );
    $query = new WP_Query ($args); // Output any matching 'found' entries
    	if ( $query -> have_posts() ) : ?>
    	<h2>Matching Finds</h2>
    	<ul>
    	<?php while ($query -> have_posts () ) : $query->the_post(); ?>
    	<li><a href="<?php the_permalink() ?>"><?php the_field('leg_ring_number') ?> (<?php the_field('date') ?>)</a></li>
    	<?php endwhile; ?>
    	</ul>
    	<?php endif; 
    wp_reset_postdata(); ?>

    Is it possible that in the first query (which should be loading $value with released ring numbers) the variable is getting overwritten with each $post and only stores the last one?