Support

Account

Home Forums ACF PRO Related Posts based on Sub-Field Values Reply To: Related Posts based on Sub-Field Values

  • OK. I found one more example and copied it almost verbatim from the ACF resource guide page here: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    I used example #4 which seemed like the closest.

    My new code is as follows:

    			<?php 
    
    			// filter
    			function my_posts_where( $where ) {
    	
    				$where = str_replace("meta_key = 'people_%", "meta_key = 'people_%", $where);
    
    				return $where;
    			}
    
    			add_filter('posts_where', 'my_posts_where');
    
    			// get the ID of the current page
    			$currentpersonpageid = get_the_ID();
    			
    
    			// args
    			$args = array(
    				'numberposts'	=> -1,
    				'post_type'		=> 'post',
    				'cat' 			=> 10,
    				'meta_query'	=> array(
    					'relation'		=> 'AND',
    					array(
    						'key'		=> 'people_%_person_page_id',
    						'compare'	=> '=',
    						'value'		=> $currentpersonpageid,
    					)
    				) 
    			);
    
    			echo $currentpersonpageid;       //debugging
    			print_r(array_values($args));    //debugging
    
    			// 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(). ?>

    I’ve inspected the current page ID and the target page ID and they are both the same value as they should be.

    I’ve inspected the $args that I created and it contains this:

    ( [0] => -1 [1] => post [2] => 10 [3] => Array ( [relation] => AND [0] => Array ( [key] => people_%_person_page_id [compare] => = [value] => 46 ) ) )

    And I’ve run the code without the properties in the meta_query array to try and isolate the problem, and the code works without that bringing back the posts in category ID 10. So all that works.

    However, when I add the key piece, the meta_query, I get zero results.
    It needs to look at all the posts in category 10, and inspect the ‘person’ repeater sub-field called ‘person_page_id’ and return any posts that match the current posts ID. But nothing.

    Any help would be much appreciated.