I have three custom 'location'
post types which make up a hierarchy of areas:
location1, location2, location3.
location1
is the top level and then location2
and location3
are childeren of each other. For example, location1
might be Buildings, location2
Floors
, and location3
Rooms.
location2
and location3
have metaboxes with fields loc2_parent_location
and loc3_parent_location
respectively. I'm looking to make an array which would populate from custom post types to show like:
Post Type Reference:
location1 > location2 > location3
Echo as:
Building 1
Building 1 > Floor 1
Building 1 > Floor 1 > Room A
Building 1 > Floor 1 > Room B
Building 1 > Floor 2
Building 1 > Floor 3
Building 2
Here is the function I'm working on:
function location_types_query ( $query ) {
// get custom post types
$args = array( 'post_type' => 'location1' );
$args = array( 'post_type' => 'location2' );
$args = array( 'post_type' => 'location3' );
// create hierachy relationships
if ($location2) {
$parent_location = get_post_meta( $post->ID, 'loc2_parent_location', true );
} elseif ($location3) {
$parent_location = get_post_meta( $post->ID, 'loc3_parent_location', true );
} elseif ($location1) {
$parent_location = '';
}
// Run the query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
foreach ($location as $locations)
if (isset($location1)) {
echo $location1;
}
if (isset($location1 && $location2)) {
echo $location1 . '>' . $location2;
}
if (isset($location1 && $location2 && $location3)) {
echo $location1 . '>' . $location2 . '>' . $location3;
}
return array($locations); // output array
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
}
?>
What should I do to make this function work properly and spit out an array of the fields.
/-------------------------------------------------------/
/* Edit 10/23 - Additional Information
/-------------------------------------------------------/
@Kaiser - Best way to break this down is as if I am making a real estate property management website. I have the post types like listed above. It's all dynamic from the options so a user could set up the system to be like above:
Building 1 Building 1 > Floor 1 Building 1 > Floor 1 > Room A
Where "building 1" is a post in cpt location1, "Floor 1" is a post in cpt location2 and "Room A" is a post in cpt location 3.
OR it can be set up like
(location1) > (location2) > (location3) // Post types Building 1 > Floor 1 > Apartment A // Posts Building 1 > Floor 1 > Apartment B
Additionally, each post type has a metabox which has values for the details of the location.
For ..
Location 1: - Location Plan - Person in charge
Location 2: - Location Plan - Person in charge - Parent Location
Location 3: - Location Plan - Person in charge - Parent Location
I've already set up the entire location database for my real estate site. What I now want to do is add a maintenance form on WP's frontend in a secure member area so tenant's can submit a maintenance request. I decided to use gravity forms with a multiple select field. I need to populate an array like above to show the different locations in the different buildings, so user can select it and then populate the field like below using the post type array I created for locations:
/*-------------------------------------------------------*/
/* Location
/*-------------------------------------------------------*/
add_filter("gform_pre_render", "gform_prepopluate_populate_location");
add_filter("gform_admin_pre_render", "gform_prepopluate_populate_location");
function gform_prepopluate_populate_location($form){
$querytype = array('location1', 'location2', 'location3')
$formid = 5;
$fieldid = 7;
if($form["id"] != $formid)
return $form;
$posts = query_posts( array( 'post_type' => $querytype ) );
$items = array(); /* Add Blank */
$items[] = array("text" => "", "value" => "");
foreach($posts as $post)
$items[] = array("value" => $post->ID, "text" => $post->post_title);
foreach($form["fields"] as &$field)
if($field["id"] == $fieldid ){
$field["choices"] = $items;
}
return $form;
}
I can then return the form results as a custom post type, say "maintenance_requests" and have the person doing the work (person responsible) view the request on their tablet and click location which pulls up the plan.