Support

Account

Home Forums General Issues Querying a Post Object relation field inside a repeater

Solved

Querying a Post Object relation field inside a repeater

  • I’m trying to build a department page for a local organization that pulls in the faculty related to that department.

    The department post is a department custom post type.
    The people associated are each a post in the people custom post type.

    • person_department = repeater field
    • person_department_select = post object field

    As any particular person may be associated with one or more departments, each Person post has a repeater field to connect that person to a department, via post object relational field.

    On the single department page template, I have the following code. This works if the relationship field is on its own, but when it’s buried in a repeater…I’m stuck at how to make that work. Help!

    
    						<?php
    							$department_people = get_posts(array(
    								'post_type' => 'people',
    								'orderby' => 'title',
    								'order' => 'ASC',
    								'meta_query' => array(
    
    									array(
    										'key' => 'person_department_select', // name of custom field
    										'value' => get_the_ID(),
    										'compare' => '='
    									),
    
    								)
    							));
    
    						?>
    						<?php if( $department_people ): ?>
    
    						<ul>
    						<?php foreach( $department_people as $department_person ): ?>
    						<?php
    
    						?>
    							<li>
    								<h4><?php echo get_the_title( $department_person->ID ); ?></h4>
    							</li>
    							<?php endforeach; ?>
    						</ul>
    						<?php endif;?>
    
    
  • Hi @charlesr

    You need to use a different code to query the posts based on the subfield. This page should give you more idea about it (Under the “4. Sub custom field values” section): https://www.advancedcustomfields.com/resources/query-posts-custom-fields/.

    We also have a guide to creating a bidirectional relationship that may fit your setup. Please check this page to learn more about it: https://www.advancedcustomfields.com/resources/bidirectional-relationships/.

    I hope this helps 🙂

  • Thanks for pointing me in the right direction. I tried taking that and modifying it for my needs, but I’ve made it so incredibly wrong.

    One issue, I think, so that I’m not properly telling it to “go to People custom post type, look for the person_department repeater field, and then give me the value for person_departmetn_select subfield -if- person_department_head field checkbox subfield is checked.”

    Here’s what I have so far…

    
    
    						<?php
    
    						// args
    						$args = array(
    							'numberposts'	=> -1,
    							'post_type'		=> 'people',
    							'meta_query'	=> array(
    								'relation'		=> 'AND',
    								array(
    									'key'		=> 'person_department_select', // this should be the first sub-field
    									'value' => get_the_ID(),
    									'compare' => '='
    								),
    								array(
    									'key'		=> 'person_department_head', // this should be the second sub-field
    									'value' => true,
    									'compare' => '='
    								)
    							)
    						);
    
    						// 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(). ?>
    
  • Hi @charlesr

    It seems that you forgot to add the functions to change the SQL query. Could you please check the link I gave you again (please check the section “4. Sub custom field values”)?

    I believe you need to do it like this:

    // filter
    function my_posts_where( $where ) {
    	
    	$where = str_replace("meta_key = 'person_department_%", "meta_key LIKE 'person_department_%", $where);
    
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    
    // args
    $args = array(
        'numberposts'	=> -1,
        'post_type'		=> 'people',
        'meta_query'	=> array(
            'relation'		=> 'AND',
            array(
                'key'		=> 'person_department_%_person_department_select', // this should be the first sub-field
                'value' => get_the_ID(),
                'compare' => '='
            ),
            array(
                'key'		=> 'person_department_%_person_department_head', // this should be the second sub-field
                'value' => true,
                'compare' => '='
            )
        )
    );

    I hope this helps 🙂

  • Thanks, James. That helped get me on my way. What threw me off were the percentage marks and incorrectly assuming that the related code only applied to dates, or was something I couldn’t modify to my purpose, to my particular variables.

    I also was wrong in how I told the query to pull up posts matching a value on a checkbox. Anyway, for the interests, here’s what I ended up using. This query looks for posts of another content type that 1) are linked through post object to the current page’s content type, and 2) either are (in this case) or aren’t checked in a checkbox indicating department head status.

    
    <?php
    
    // filter
    function my_posts_where( $where ) {
    	$where = str_replace("meta_key = 'person_department_%", "meta_key LIKE 'person_department_%", $where);
    	return $where;
    }
    add_filter('posts_where', 'my_posts_where');
    
    	// checks if there's a department head
    	// if so, displays that person
    
    	// args
    	$args_faculty_head = array(
    		'posts_per_page'	=> -1,
    		'post_type'		=> 'people',
    		'orderby' => 'title',
    		'order' => 'ASC',
    		'meta_query'	=> array(
    			'relation'		=> 'AND',
    			array(
    				'key'		=> 'person_department_%_person_department_select', // this should be the first sub-field
    				'value' => get_the_ID(),
    				'compare' => '='
    			),
    			array(
    				'key'		=> 'person_department_%_person_department_head', // this should be the second sub-field
    				'value' => '"Yes"',
    				'compare' => 'LIKE'
    			)
    		)
    	);
    
    	// query
    	$the_query = new WP_Query( $args_faculty_head );
    	?>
    
    	<?php if( $the_query->have_posts() ): ?>
    
    		<ul>
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    			<li>
    				<?php the_title(); ?>
    			</li>
    		<?php endwhile; ?>
    		</ul>
    	<?php endif; ?>
    	<?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>
    
    
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.