Support

Account

Home Forums General Issues Relationship query help Reply To: Relationship query help

  • Hi @sccr410

    Sorry about that, I read your above comment and though you were using a repeater field for each doctor!

    Please don’t use a repeater field for this. Continue to use the 2 post types related via a relationship field.

    Okay, so to achieve the original question, you will need to perform 2 seperate queries on the DB.

    The first query is to find the locations within the radius. There are a few tutorials on stack overflow which talk about SQL radius calculations which I have done once before on a complicated search directory and it did work correctly.

    Next, once you have all your locations, you can build up the meta_query args for each doctor.

    Taking straight from the docs, to find a doctor which is connected to location 2, you would have:

    
    <?php 
    
    $doctors = get_posts(array(
    	'post_type' => 'doctor',
    	'meta_query' => array(
    		array(
    			'key' => 'location',
    			'value' => '"' . 2 . '"', 
    			'compare' => 'LIKE'
    		)
    	)
    ));
    
     ?>
    

    So to find any doctors working in location2, 3, or 4, you would have:

    
    <?php 
    
    $doctors = get_posts(array(
    	'post_type' => 'doctor',
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'location',
    			'value' => '"' . 2 . '"', 
    			'compare' => 'LIKE'
    		),
    		array(
    			'key' => 'location',
    			'value' => '"' . 2 . '"', 
    			'compare' => 'LIKE'
    		),
    		array(
    			'key' => 'location',
    			'value' => '"' . 3 . '"', 
    			'compare' => 'LIKE'
    		)
    	)
    ));
    
     ?>
    

    To produce this meta_query args, you can simply loop over the results from DB query #1.

    Hope that helps.

    Thanks
    E