Support

Account

Forum Replies Created

  • Thanks for your reply. I was being an egit and not including the acf_form_head() in the necessary page template. Thank you!

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

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

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

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

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

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

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

  • Thanks James. Can I ask a related question to see if there is a simpler way to solve the same problem?

    Is it possible to store the value of a particular custom field (for all applicable posts) as a variable that could then be used as the ‘value’ in a meta_query?

    In my context that would mean storing all ring numbers (from all users) as a variable to be compared to the released ring numbers of the signed-in user.

  • It’s all sorted now. I think it was a bit of schoolboy error in trying to implement V5 code with the V4 plugin. All working nicely now that I’m using Pro. Thanks for the reply.

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