Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Mapping Specific Posts By ID WordPress

I have a comma separated list of IDs of posts in a custom post type called retailers, that I am putting in a variable called $retailers. I have confirmed this list is correct. All posts in retailers have been successfully geocoded.

I am trying to output a map of just those posts. I have tried:


$map_args = array (
'object_name' => 'post',
'map_post_type' => 'retailers',
'object_ids' => $retailers,
'width'=>'800px',
);
echo GeoMashup::map( $map_args );


and


$args = array(
'post_type' => 'retailers',
'post__in' => array( $retailers ),
);
$query = new WP_Query( $args );

$map_args[ 'content' ] = $query;
echo GeoMashup::map( $map_args );



In both cases, I get all posts in retailers on the map.

Answers (4)

2014-07-09

Luis Abarca answers:

Check if posts (retailers) are sticky, remember, WP_Query posts_in will get the posts in the array plus the sticky posts prepended.


JPollock412 comments:

There are no sticky posts in retailers.

2014-07-09

Romel Apuya answers:

$retailers should be an array and you said the variable is delimited by comma(,)
so you need to convert it to array using explode.

$retailers_array = explode(",",$retailers);
$args = array(
'post__in' => $retailers_array
);
$query = new WP_Query( $args );

$map_args[ 'content' ] = $query;
echo GeoMashup::map( $map_args );


JPollock412 comments:

I'm actually getting that comma separated list by imploding an array. I tried skipping the implosion, no difference. I also tried hardcoding the list with IDs I know to be good, both for 'post__in' for WP_QUERY and for 'object_ids'


Romel Apuya comments:

Have you tried omitting the 'post_type' => 'retailers', in the query??


JPollock412 comments:

I just did. No difference, still getting all retailers.


Romel Apuya comments:

can you provide screenshot of the variable dump of retailers?
after the implode?

var_dump($retailers);


JPollock412 comments:

Sure.
http://imgur.com/8iWKUB7

The first var_dump is of $retailers. What you see below it is a var_export generated using the 'geo_mashup_locations_where' action. The post ID you see in the IN() at the end of it is the current post ID.


Romel Apuya comments:

ok add wp_reset_query();
after the
echo GeoMashup::map( $map_args );


JPollock412 comments:

I solved this using the 'geo_mashup_locations_where', but thanks for your help.


add_action('geo_mashup_locations_where', function($a ){
global $pods;
$retailers = $pods->field( 'current_retailers.ID' );
$retailers = implode( ',', $retailers );
return 'WHERE post_status = \'publish\' AND o.post_type IN (\'retailers\') AND gmlr.object_id IN ( '.$retailers.' )';
}

);

2014-07-09

timDesain Nanang answers:

are you sure this code is working :

$args = array(
'post_type' => 'retailers',
'post__in' => array( $retailers ),
);


i think $retailers must be an <strong>array </strong> type

$retailers = array( 2, 3, 5, 7, 11 );
$args = array(
'post_type' => 'retailers',
'post__in' => $retailers,
);


if "All posts in retailers have been successfully geocoded", then what should we do?
:)


JPollock412 comments:

I tested that by doing:



if ( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
$ids[] = $query->post->ID;
}
print_r( $ids );
}


I got the same IDs as I put in.


timDesain Nanang comments:

are you using this plugin: https://wordpress.org/plugins/geo-mashup/ ?

try to
- create custom taxonomy (asume: retailer_city)
- input a city (asume: New York)
- assign the $retailers (your IDs of posts) to New York
- try this code to show all retailers in New York City:


$args = array(
'post_type' => 'retailers',
'tax_query' => array(
array(
'taxonomy' => 'retailer_city',
'field' => 'slug',
'terms' => 'new-york',
)
)
);
$query = new WP_Query( $args );
$map_args[ 'map_content' ] = $query;
echo GeoMashup::map( $map_args );


timDesain Nanang comments:

if you want to show retailers in multiple cities, just try :


$args = array(
'post_type' => 'retailers',
'tax_query' => array(
array(
'taxonomy' => 'retailer_city',
'field' => 'term_id',
'terms' => array( 14, 15),
)
)
);


or

$args = array(
'post_type' => 'retailers',
'tax_query' => array(
array(
'taxonomy' => 'retailer_city',
'field' => 'slug',
'terms' => array( 'atlantic-city', 'millvile' ),
)
)
);


timDesain Nanang comments:

but, if you want show retailers by ids, try this code


$retailers = "199,203, 204";
$retailers = explode(',', $retailers);
$arr_ret = array();
foreach($retailers as $r){
$arr_ret[] = (int) $r;
}

$args = array(
'post_type' => 'retailers',
'post__in' => $arr_ret,
);
$query = new WP_Query( $args );
$map_args[ 'map_content' ] = $query;
echo GeoMashup::map( $map_args );


hope this will help.


JPollock412 comments:

I think you're getting hung up on how I'm getting that array of IDs.

This produces the same results (all posts, which is the global map) as before:

$args = array(
'post__in' => array( 6915,6913),
);
$query = new WP_Query( $args );
$map_args = array( 'content' => $query );
echo GeoMashup::map( $map_args );



6915 and 6913 are poosts in retailer that have geocoded values.


JPollock412 comments:

I solved this using the 'geo_mashup_locations_where', but thanks for your help.


add_action('geo_mashup_locations_where', function($a ){
global $pods;
$retailers = $pods->field( 'current_retailers.ID' );
$retailers = implode( ',', $retailers );
return 'WHERE post_status = \'publish\' AND o.post_type IN (\'retailers\') AND gmlr.object_id IN ( '.$retailers.' )';
}

);


timDesain Nanang comments:

it seem you are not using the plugin above.

you need wp primary query and Geo Mashup with contextual map :
info: http://snipplr.com/view/37962/

look like your map return a Global Map (If the post or page has no location, a Global Map of all located posts and pages is displayed.)
info: https://code.google.com/p/wordpress-geo-mashup/wiki/Documentation#Single_Map

try this code

$args = array(
'post_type' => 'retailers',
'post__in' => array( 6915,6913 ),
);
query_posts( $args );
echo GeoMashup::map( 'map_content=contextual' );

wp_reset_query();





2014-07-09

Dylan Kuhn answers:

You shouldn't have to build the where clause yourself. Looking over your other replies above, here's the code I think should do it:


$retailers = implode( ',', $retailers );
$map_args = array (
'map_content' => 'global',
'object_name' => 'post',
'object_ids' => $retailers,
);
echo GeoMashup::map( $map_args );


You can include the map_post_type argument as well, but it isn't necessary when you have post IDs already.

The gotcha is that map_content is required. I think Geo Mashup is choosing the wrong value for this in this case - I'll have a look at that.


Dylan Kuhn comments:

You shouldn't have to build the where clause yourself. Looking over your other replies above, here's the code I think should do it:


$retailers = implode( ',', $retailers );
$map_args = array (
'map_content' => 'global',
'object_name' => 'post',
'object_ids' => $retailers,
);
echo GeoMashup::map( $map_args );


You can include the map_post_type argument as well, but it isn't necessary when you have post IDs already.

The gotcha is that map_content is required. I think Geo Mashup is choosing the wrong value for this in this case - I'll have a look at that.


JPollock412 comments:

Dylan-

Thank you, that works, except one thing. As you can see in this screenshot it loads as a grey box, which I can click to see the map that I expect:

http://imgur.com/SUmcRL4

The print_r you see in the image is generated with this:


add_action('geo_mashup_locations_where', function($a ){
print_r( $a );
});


I am outputting the map like this:

$retailers = $pods->field( 'current_retailers.ID' );
$retailers = implode( ',', $retailers );
$map_args = array (
'zoom' => 8,
'map_content' => 'global',
'object_name' => 'post',
'object_ids' => $retailers,
'map_post_type' => 'retailers'
);
echo GeoMashup::map( $map_args );


Removing the 'zoom' or the 'map_post_type' argument has no effect.

Take care,
Josh


Dylan Kuhn comments:

That looks like the 'Click to Load' feature. You could uncheck that in Settings / Geo Mashup / Global Maps, or send the click_to_load parameter:


$map_args = array (
'zoom' => 8,
'map_content' => 'global',
'object_name' => 'post',
'object_ids' => $retailers,
'map_post_type' => 'retailers',
'click_to_load' => false,
);