Support

Account

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

Solved

Using get_field to set a variable with values from a user

  • There may be a really obvious problem here, but I’m struggling to see it.

    I want to set a variable with all the values from a particular field and user (multiple posts). Here’s my code using get_field:

    $post_id = "user_365";
    $field = get_field( 'ring_numbers', $post_id ); 
    echo "<p>" . $field . "</p>" ; 
    

    This words if the $post_id is set to a particular post ID but not users, or categories. What am I missing?!

  • I’m not sure I understand. Is the field ‘ring_numbers’ a field that you edit with editing a user profile or a post of some kind?

  • The ‘ring_numbers’ are an ACF field which I’m attempting to combine into a variable in order to use them in a meta_query. It would be useful to be able to do this for a user and also for a particular category of post.

    The variable is then used as the value in a meta_query in order to find matching posts from a different category.

    Sorry if I’m not being very eloquent in my explanation. Thanks.

  • Here’s the full code how I think it should work (but doesn’t!).

    <?php
    $post_id = "user_365";
    $userrignnumbers = get_field( 'ring_numbers', $post_id ); // Get all the 'released' bird ring numbers entered by this user 
    
    $args = array (
    	'post_type' => 'wglocations',
    	'posts_per_page'  => -1,
    	'category_name' => 'wg-finding',
    	'meta_query' => array (
    		array (
    			'key' => 'leg_ring_number',
    			'value' => $userrignnumbers, // 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() ) : ?>
    	<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(); ?>
  • The context is this:

    (1) Members release endangered birds into the wild each with a unique leg ring. Every release (and leg ring number) is logged using ACF front end posting on the website.

    (2) When birds are found with a leg ring, the finder is also invited to log the location and leg ring number on the website, also using ACF.

    (3) When a bird with a matching leg ring number to a release is found, the member who released the bird is notified.

  • So, `ring_numbers’ is a filed on the user profile page. It appears to be returning an array, or at least you think is should be. What type of a field is this?

    Then….

    What would be easier is if you export the two field groups that the fields ring_numbers and leg_ring_number are in. Not sure what version of ACF you’re using, but if you can create a file an put it in a .zip arcive you can attach it here (paperclip icon to the left of the message field). That would probably give me a better idea what you’re working with than trying to explain it.

  • Thanks so much, please find the file attached.

    A few important notes:

    • To answer your question above, no, both the ring_numbers and leg_ring_number fields are filled in on custom posts (using front end posting) not the user profile page.
    • You’ll notice that the release field group contains both a text field called ring_numbers and a repeating sub field called ring_numbers_repeater_sub. They aren’t both necessary but one of them needs to work!
    • There’s a lot of additional fields in both groups which aren’t important for this functionality either, and can be ignored.
    • I’ve been trying to solve the problem using a meta_query but if you think there’s a more obvious way of solving it, I’m very open to suggestions!

    Thanks again.

  • I also meant to mention that I’m using Version 5.3.5 of ACF.

  • I took a look at the field groups. The user is not associated with either of the and the fields are not saved to the user in any way, as you said, they are saved to a custom post type.

    The other problem that I see is that on the “Release” post, there can be multiple ring numbers, either a comma separated list of in a repeater. This is going to be a problem to search.

    It would be easier to do the search when viewing the release page/post to search for the found pages/posts.

    
    // get ring numbers
    // $post_id is the post ID of the release page/post 
    // in the 'wglocations' post type
    
    // this first code is for the comma separated list
    $value = get_field('leg_ring_number', $post_id);
    
    // just in case they put spaces after commas, remove them
    $values = str_replace(' ', '', $ring_numbers);
    
    $ring_numbers = explode(',', $value);
    
    // now you have the values in the right format and
    // you can search for matching found numbers
    // the field on the leg_ring_number on post type wglocations
    // can only allow one number
    
    $args = array(
      'post_type' => 'wglocations',
      'posts_per_page' => -1,
      'category_name' => 'wg-finding',
      'meta_query' => array (
        array (
          'key' => 'leg_ring_number',
          'value' => $ring_numbers, // Use the user's 'released' ring numbers
          'compare' => 'IN', // Compare them to any 'found' ring numbers
        )
      )
    );
    $query = new WP_Query ($args);
    // the rest of you code for displaying them continues here
    
  • Thanks so much for your time on this.

    That code works nicely on a release post (though I needed to use $value in the meta query, not $ring_numbers).

    However, this requires the user to check all his release posts in order to find out if there are any matches. What I was really looking for was a way of collating all his release ring numbers (across multiple posts) into a single variable that could then be used in a meta query on a member’s ‘home page’.

    But is this not possible? Thanks again.

  • So then you’ll need to query the ‘wglocations’ to get all the posts for the user and then loop through those to build a list of all the numbers to look for

    
    $args = array(
      'post_type' => 'wglocations',
      'post_per_page' => -1,
      'author' => $user_id
    );
    $query = new WP_Query($args);
    $ring_numbers = array();
    foreach ($query->posts as $post) {
      $value = get_field('leg_ring_number', $post_id);
      $values = str_replace(' ', '', $ring_numbers);
      $ring_numbers = array_merge($ring_numbers, explode(',', $value));
    }
    

    Then you do the second query from the previous code

  • 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?

  • 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.

  • Sorry I did not get back to you sooner, for some reason your last to replies ended up in my spam folder and I just say them.

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

You must be logged in to reply to this topic.