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

  • Done it. I needed to use .= not just = in order to concatenate (add) the strings to the variable ($value) and not replace them on each loop.

    Here’s the successful code:

    <?php
    $args = array( // Get all posts of a certain type and category, by the logged in user.
      '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') . ', '; // This puts all the ring_numbers fields into one long string, separated by commas.
      $values = str_replace(' ', '', $ring_numbers);
      $ring_numbers = array_merge($ring_numbers, explode(',', $value));
    }
    			
    $args = array( // We then get all posts of a certain type and category from all users...
      'post_type' => 'wglocations',
      'posts_per_page' => -1,
      'category_name' => 'wg-finding',
      'meta_query' => array ( // ... and compare them to the $value variable produced above.
        array (
          'key' => 'leg_ring_number',
          'value' => $value,
          'compare' => 'IN',
        )
      )
    );
    $query = new WP_Query ($args); // Output any matching entries
    	if ( $query -> have_posts() ) : ?>
    	<div class="boxhighlight">
    		<h2>Matching Finds</h2>
    		<p><em>The following released bird(s) have been found. Click on the link(s) below for more information.</em></p>
    		<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>
    	</div>
    	<?php endif; 
    wp_reset_postdata(); ?>

    Hope this is of help to anyone else looking for a similar solution. If there’s a cleaner way of getting the same result I’d be keen to see it.

    Thanks particularly to John Huebner for all his kind and generous help on this.