Support

Account

Home Forums Add-ons Repeater Field If Subfields Equals, Show Posts

Solved

If Subfields Equals, Show Posts

  • Hey, folks,

    I’ve been researching this on the forum and elsewhere but have yet to stumble across an example of what I’m trying to achieve at the template level.

    What I need to do is use a conditional statement that does the following:

    During a query, if the values of two subfields in a repeater field equals a specific selection, then those posts are displayed. Here the first subfield’s meta key would be ‘year’ and the value ‘2015’, and the other meta key would be ‘level’ with a desired value of ‘grand prize’. Only those posts whose values match the two keys would be rendered.

    The closest example I’ve found of this in use is the Query posts with ACF values one over at http://www.advancedcustomfields.com/resources/code-examples/ but it only refers to a single subfield.

    Any help or direction would be greatly appreciated!

  • Understanding how to do this would be very useful! +1

  • Yeah. I’ve been Googling the heck out of this and have yet to find a solution.

  • Here’s my code to show where I’m stuck. Note that this is using one meta value and not more than one, which is what I’m looking for. This code doesn’t work. It ignores the meta name and value in the args array.

    <?php 
    	$args = array(
        'posts_per_page' => 1,
        'post_type' => 'profiles',
    	'orderby' => 'ID',
    	'order' => 'DSC');
    	'meta_name' ==> 'year', //not working
    	'meta_value' ==> '2015', //not working
    	query_posts($args);
    ?>
    
    <?php while ( have_posts() ) : the_post(); ?>
    			
    <section class="winner-level">
    	<h3><a>"><?php the_title(); ?></a></h3>
    	<?php if(get_field('awards_given')): ?>
      	<?php while(has_sub_field('awards_given')): ?>
    	
    	<?php if( get_sub_field('year') ): ?>
    		
    	<p><?php the_sub_field('program'); ?> - <?php the_sub_field('program_category'); ?></p>
    	<p><em><?php the_sub_field('campaign_title'); ?></em></p>
    	<p><strong><?php the_sub_field('level'); ?></strong></p>
    		
    	<?php endif; ?>
     
    	<?php endwhile; ?>
     	<?php endif; ?>
    </section>
    
    <?php endwhile; ?> 
    
    <?php wp_reset_query(); ?>
    

    Cheers!

  • Hi @fredjs and @chromasites

    There are some information regarding querying subfield here: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/


    @fredjs
    first off you should never use query_posts.. it’s been shunned by developers for quite some time. Instead use either pre_get_posts filter to modify a core query or the WP_Query class to create a new query.

    You have not told me the fieldname of your repeater field so in this example it’s written as “repeaterfieldname”. For the examples to work you need to replace these.

    Put this in the template where you want to display the posts:

    
    <?php
    // args
    $args = array(
    	'posts_per_page'	=> -1,
    	'post_type'		=> 'profiles',
    	'meta_query'	=> array(
    		'relation'		=> 'AND',
    		array(
    			'key'		=> 'repeaterfieldname_%_year',
    			'compare'	=> '=',
    			'value'		=> '2015',
    		),
    		array(
    			'key'		=> 'repeaterfieldname_%_level',
    			'compare'	=> '=',
    			'value'		=> 'grand prize',
    		)
    	)
    );
    
    // query
    $the_query = new WP_Query( $args );
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<ul>
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
    
    <?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>
    

    And this should go in your themes functions.php:

    
    // filter
    function my_posts_where( $where ) {
    	
    	$where = str_replace("meta_key = 'repeaterfieldname_%", "meta_key LIKE 'repeaterfieldname_%", $where);
    
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    

    Please note that if your sub fields are in different repeaters you need to do a replace on both of these in the my_posts_where function:

    
    // filter
    function my_posts_where( $where ) {
    	
    	$where = str_replace("meta_key = 'repeaterfieldname_%", "meta_key LIKE 'repeaterfieldname_%", $where);
    $where = str_replace("meta_key = 'repeaterfieldname2_%", "meta_key LIKE 'repeaterfieldname2_%", $where);
    
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    

    Try it out!

  • Hi, Jonathan,

    First of all, thanks for the input! I know what you mean about the deprecated code. I’m working on a WP site that someone else built. Not an excuse, but yeah.

    I used the first function you posted since I’m using only one repeater in my group. I tried using the code you posted but it’s not showing any posts in the desired area of the page. The page loads the rest of the content so there is that.

    Here’s the code I’m using based on what you shared:

    <?php
    // args
    $args = array(
    	'posts_per_page'	=> 1,
    	'post_type'		=> 'profiles',
    	'meta_query'	=> array(
    		'relation'		=> 'AND',
    		array(
    			'key'		=> 'repeaterfieldname_%_year',
    			'compare'	=> '=',
    			'value'		=> '2015',
    		),
    		array(
    			'key'		=> 'repeaterfieldname_%_level',
    			'compare'	=> '=',
    			'value'		=> 'GRAND PRIZE',
    		)
    	)
    );
    
    // query
    $the_query = new WP_Query( $args );
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    	
    	<section class="winner-level">
    		<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    		<?php if(get_field('awards_given')): ?>
      		<?php while(has_sub_field('awards_given')): ?>
    	
    		<?php if( get_sub_field('year') ): ?>
    		
    		<p><?php the_sub_field('program'); ?> - <?php the_sub_field('program_category'); ?></p>
    		<p><em><?php the_sub_field('campaign_title'); ?></em></p>
    		<p><strong><?php the_sub_field('level'); ?></strong></p>
    		
    		<?php endif; ?>
     
    		<?php endwhile; ?>
     		<?php endif; ?>
    	</section>
    
    <?php endwhile; ?>
    <?php endif; ?>
    
    <?php wp_reset_query();	?>
    

    Thoughts? Note that ‘GRAND PRIZE’ is in caps in the repeater field settings, which is why I made it such in the array.

    Cheers

  • Hi @fredjs

    I think you need to read my instructions again πŸ™‚
    You have to replace the “repeaterfieldname” in the code with your actual repeater fields name.

    And of course you need to add the posts_where filter to functions.php (AND replace repeaterfieldname there as well).

  • I realized that on the way home yesterday when thinking about it. Wow, that was a dumb oversight. I’ll try it and get back to ya. Thanks. πŸ˜‰

  • Hi, Jonathan,

    Works like a charm! I can’t thank you enough for the guidance!

    Have a great day!

  • Great to hear!

    Hopefully I was able to help @chromasites as well?

    Best of luck in your further development.

  • @jonathan:

    I think I’m having exactly the same problem as @fredjs, (just a bit simpler, I’d dare to say), yet still did not manage to succeed. I have just posted my question here.

    Would you take a look and give advice?

    Appreciate, @jonathan! keep up the great work

    Alessio

  • Hi @jonathan,

    I know this is an old thread, but your explanation and solution were nice and clear, so I’m hoping you’re still around for follow-up…

    What if the sub-field of the repeater is a relationship field?
    In that case what’s the proper way to set the meta_value for matching?

    Going on the example above, let’s say the value is not simply a string like “GRAND PRIZE” but rather an instance of a custom post type called “Prize Levels”.

    Thanks,
    Alison

  • Following up on my previous post, I’ve found the solution through a number of different sources. Noting it here in case anyone else has the same question:

    I was daunted by the fact that relationship info was stored as a serialized array e.g.
    a:2:{i:0;s:2:”35β€³;i:1;s:2:”33β€³;}
    but this post makes it clear that it’s as simple as putting the ID of the sought-after post in quotation marks (for exact match) and doing a LIKE comparison, e.g.:

    'meta_query'	=> array(
        array(			
            'key' => "repeaterfieldname_XYZ_subfieldname",
            'compare' => 'LIKE',
            'value' => '"' . $post_id . '"', // matches exactly "123". This prevents a match for "1234"
        )
    )

    (in other words, since the serialized array is treated just like any other string in running the DB query, you just search for the post ID as a fragment of that string.)

    Note that due to changes in WordPress, you can no longer use the percent sign as your wildcard. The percent sign will be substituted with a hash and your query will come up empty. So, see http://www.advancedcustomfields.com/resources/query-posts-custom-fields/ for a related example, which suggests using the dollar sign instead.

    Since the dollar sign is used to indicate variable names in php, though, and can therefore cause confusion if you’re using a text editor with syntax highlighting, I prefer to use a string like XYZ for the substitution instead.

    So my filter is as follows:

    function my_posts_where( $where ) {
    	
    	$where = str_replace("meta_key = 'repeaterfieldname_XYZ", "meta_key LIKE 'repeaterfieldname_%", $where);
    
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');
Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘If Subfields Equals, Show Posts’ is closed to new replies.