This is the url: HOUSE 1

I have custom post type "Properties".For each new property, I can select "Property Type", "Property Status", "Property Location" taxomony. This page is using its own template, so I was wondering - can I modify the query for that template to show other properties from one "Property Location", that would match the current Location?

For example, if I open any "House 1" ("House 1" is Property Location) property, I don't want the page to show posts with ""House 2" Property Location, but posts from the current Location only.

I tried few combinations using http://generatewp.com/wp_query/ , but can't figure this out. I am sure, though, that it's a simple thing to change.

  • I tried few combinations, just guessing what might work, with no actual understanding what I need to do... – Bruno Kos Jun 30 '14 at 10:47
  • 1
    What you are using as location identifier, Is it a post meta or taxonomy? – TBI Infotech Jun 30 '14 at 10:50
  • 1
    Bruno, please post the results of what combinations you've generated with generatewp.com. There probably won't be anyone helping you unless you show what you've tried yourself, what results you've got and what went wrong. – engelen Jun 30 '14 at 11:06
up vote 1 down vote accepted

If you are already on the page of the house (in the loop of the post) you want then something like this should work. I imagine the values are saved as post meta???

If so I had to do something similar and managed to achieve it like this although I'm sure someone out there may have a better way round.

$ThisPostID = get_the_ID();  //simply gets the current post ID
$TheHouseLocation = get_post_meta( $ThisPostID, 'property_location' ); //property_location is probably the meta key name this can be checked with a simple post meta inspector plugin. If so this gets the current 'location' of the house being viewed

 $args=array(
        'post_type' => 'Properties',
        'post_status' => 'publish',
        'meta_key' => 'property_location',
        'meta_value' => $TheHouseLocation,
        'posts_per_page' => -1,
        'ignore_sticky_posts'=> 1,
        );

 $ShowSomeHouses = new WP_Query($args);
 if ($ShowSomeHouses->have_posts()) {
//This bit happens if there are houses in same location look for the else below if there are none.
while ($ShowSomeHouses->have_posts()) : $ShowSomeHouses->the_post();

You could cycle through the post details using calls such as the_title(); or the_content(); here.

OR ADD

$NearbyHouseIDs = get_the_ID(); //This should find the post id's of the houses in same location.
foreach ($NearbyHouseIDs as $nearbyhouse) {

This middle bit gives you the chance to format the display or layout as you wish but by the sounds of it there is already some layout in the template you are using. As I have no idea what the original code is I won't try and recreate it here. the $nearbyhouse is the post ID number of each of those houses found in the area. You can then call custom details like meta & other information using something like get_post_meta( $nearbyhouse, $key, $single );

}
endwhile;
} else echo '<p>There are no nearby houses to show at the moment</p>';
wp_reset_query();  // Turns everything back to normal

DISCLAIMER: This is just a bit of code written on a blog - I have not tested it nor do I claim to be a skilled or gifted coder.

You say your locations are categories, right? So I'ld recommend to have a look at get_posts() to get an array of certain posts. You can pass category as one argument to get_posts. So you'ld only need to get the category of the current post and then put it as an argument of get_posts().

Maybe you'ld have a look at Flexible Posts Widget then as well for outputting the stuff you want in a widget anywhere you like. Just look a the default template it comes along with. I'll bet you get the idea very quickly :)

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.