Home › Forums › ACF PRO › Query Custom-Post-Type by it's map sub field address(es), within another query
I’m trying to query a custom post type using a repeating Google map sub-field, but I’m trying to do this within another query, for the same custom post type (if this makes any sense at all).
Long story short…
Here’s what I have so far…
<div class="acf-map">
<?php
$args = array(
'post_type' => 'members',
'post_status' => 'publish',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php while ( have_rows('which_countries') ) : the_row();
$location = get_sub_field('country');
?>
<?php // start marker ?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<p><?php echo $location['address']; ?></p>
<?php
// filter
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'which_countries_$", "meta_key LIKE 'which_countries_%", $where);
return $where;
} // THIS SEEMS TO BREAK THE LAYOUT?
add_filter('posts_where', 'my_posts_where');
// args
$args = array(
'posts_per_page' => -1,
'post_type' => 'members',
'post_status' => 'publish',
'meta_query' => array(
'key' => 'which_countries_$_country',
'value' => $location['address'],
'compare' => '='
)
);
// query
$the_query = new WP_Query( $args ); ?>
<?php if( $the_query->have_posts() ): while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-xs-6">
<a href="<?php the_permalink(); ?>"><?php the_title(); echo ' - ' . $location['address']; ?></a>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
<?php // end marker ?>
<?php endwhile; ?>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
I’ve looked at the documentation for example 4 from this page: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/#custom-field parameters
But, this hasn’t helped.
Removing the lines on filtering adds all the correct pins to the map, but also all posts.
The custom field names are ‘which_countries’ and ‘country’. I’ve also output the meta data to the page to see key names and have the following example:
[which_countries_0_country] => Array ( [0] => a:3:{s:7:”address”;s:12:”Scotland, UK”;s:3:”lat”;s:17:”56.49067119999999″;s:3:”lng”;s:19:”-4.2026458000000275″;} ) [_which_countries_0_country] => Array ( [0] => field_5a8eba7f3e08f ) [which_countries_1_country] => Array ( [0] => a:3:{s:7:”address”;s:7:”Ireland”;s:3:”lat”;s:8:”53.41291″;s:3:”lng”;s:18:”-8.243889999999965″;} ) [_which_countries_1_country] => Array ( [0] => field_5a8eba7f3e08f )
Clearly I have either an error or omission somewhere, but can’t seem to get my head round it.
Any help is greatly appreciated!
Thanks.
Updating the arguments of the inner query has fixed the broken layout, there is actually HTML after the p tag in the marker, but still only showing the one marker and no custom post types in said marker. I’d missed the array inside an array for the meta query, which has now been updated.
// args
$args = array(
'posts_per_page' => -1,
'post_type' => 'members',
'post_status' => 'publish',
'meta_key' => 'which_countries',
'meta_value' => $location['address'],
'meta_query' => array(
array (
'key' => 'which_countries_$_country',
'value' => $location['address'],
'compare' => 'LIKE'
)
)
);
I’ve also updated the comparison to LIKE, but again no joy.
Anyone any thoughts on this?
The topic ‘Query Custom-Post-Type by it's map sub field address(es), within another query’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.