Support

Account

Home Forums General Issues Querying a relationship field with a custom field filter

Solved

Querying a relationship field with a custom field filter

  • I’m trying something new here and could use a bit of help in achieving the result.

    To illustrate my point, the example from this site on relationship field queries works well. The one with doctors and locations.
    https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    In this example, towards the bottom, in single-location.php, the page lists doctors by location. So Melbourne has doctors 5, 3, and 2.

    What if in the doctor individual posts (for the doctor custom post type), there’s a radio button type field for…say, whether the doctor is male or female.

    How could I include this to the query above so that it only pulls up the doctors in the Melbourne location that are male?

    For reference, here’s the code from the tutorial to the above doctors by location example.

    
    				<div class="entry-content">
    						<h2>Address</h2>
    						<p><?php the_field('address'); ?></p>
    
    						<h2>Doctors that work here</h2>
    						<?php 
    
    						/*
    						*  Query posts for a relationship value.
    						*  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
    						*/
    
    						$doctors = get_posts(array(
    							'post_type' => 'doctor',
    							'meta_query' => array(
    								array(
    									'key' => 'location', // name of custom field
    									'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    									'compare' => 'LIKE'
    								)
    							)
    						));
    
    						?>
    						<?php if( $doctors ): ?>
    							<ul>
    							<?php foreach( $doctors as $doctor ): ?>
    								<?php 
    
    								$photo = get_field('photo', $doctor->ID);
    
    								?>
    								<li>
    									<a href="<?php echo get_permalink( $doctor->ID ); ?>">
    										<img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>" width="30" />
    										<?php echo get_the_title( $doctor->ID ); ?>
    									</a>
    								</li>
    							<?php endforeach; ?>
    							</ul>
    						<?php endif; ?>
    
    					</div>
    
  • Hi @charlesr

    In that case, you need extra parameters to query the doctors like this:

    $doctors = get_posts(array(
        'post_type' => 'doctor',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'location', // name of custom field
                'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'gender', 
                'value' => 'male', 
                'compare' => 'LIKE'
            )
        )
    ));

    If you want the radio button to act like a filter, please take a look at this page to learn more about filtering posts with ACF: https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    I hope this helps 🙂

  • That did the trick. Thanks! That helps me be on my way with this project.

    And thanks for pointing me toward that filtering page, I’m sure that will come in handy.

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

The topic ‘Querying a relationship field with a custom field filter’ is closed to new replies.