I'm trying to extract some data from a child (unit) of a parent (building) in order to show two different things. First, I want to grab the number of beds available from the rental unit from low to high (i.e. 1 & 2 beds).
The next part of my problem, is returning the lowest rent value to display as 'rents from $'.
So for the Beds problem, I have this
// beds
$child_posts = types_child_posts('unit', array('numberposts'=>2,));
$count = 0;
foreach ($child_posts as $child_post) {
$beds_id = $child_post->ID;
$beds = strip_tags(get_the_term_list( $beds_id, 'beds'));
if ($count < 1){
echo $beds.' & ';
$count ++;
} else {
echo $beds.' beds ';
}
}
for the rent problem, I have this...
$child_posts = types_child_posts('unit', array('numberposts'=>1,'orderby'=>'post_date','order'=>'ASC'));
foreach ($child_posts as $child_post) {
echo '<span data-icon="" class="icondim"></span> from $'.$child_post->fields['rent-per-month'].'/month'; */
}
?>
No matter what I've tried, I can't seem to return anything but the last child post (so I get 2&2 beds and the highest rent).
What am I missing??
Dear Malcolm,
I would suggest that you use the following to retrieve the number of beds from the rental unit provided that your on the parent building page.
$output = '';
$all_child_posts = types_child_posts('unit');
$number_of_beds = array();
foreach ($all_child_posts as $child_post) {
$number_of_beds[] = (int)get_post_meta( $child_post->ID, 'wpcf-number-of-beds', true );
}
//display only distinct values
$number_of_beds = array_unique( $number_of_beds );
//sort low to high
asort( $number_of_beds );
//select the lowest two to display
$number_of_beds = array_slice( $number_of_beds, 0, 2 );
$single_plural = count($number_of_beds);
if ( $single_plural == 1 ) {
$single_plural = $number_of_beds[0];
} else if ( $single_plural == 0 ) {
$number_of_beds[] = 0;
}
$output = implode( ' & ', $number_of_beds) . ' ' . _n( 'bed', 'beds', $single_plural );
echo $output;
With this code you have an ordered list from low to hight and displaying the lowest 2 items, also the number of beds in the unit are saved in a custom field 'wpcf-number-of-beds'.
For the rental per month I would suggest something like:
$all_child_posts = types_child_posts('unit');
$rent_per_month = array();
foreach ($all_child_posts as $child_post) {
$rent_per_month[] = (int)get_post_meta( $child_post->ID, 'wpcf-rent-per-month', true );
}
//sort low to high
asort( $rent_per_month );
//select the lowest to display
$rent_per_month = array_slice( $rent_per_month, 0, 1 );
$rent_per_month = ( isset($rent_per_month[0]) ? $rent_per_month[0] : '' );
$output = '<span data-icon="" class="icondim"></span> ' . sprintf( __('from $%d/month'), $rent_per_month );
Here you have only listed the lower price.
Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Ana
Thanks Ana,
I think I see where you are going...but as it stands the code returns '0 beds' and nothing at all for the 'rent from'.
I should point out that what I am making is a summary from the various buildings that are returned in the archive.php page based on location (i.e. all of the buildings from a specific city).
Any other suggestions?
Update Ana,
The code for the 'rent from' does work, I just needed to echo it.
Still having problems with the number of beds. While I have you, I notice your code grabs the two lowest values...is it possible to grab a third if need be? (in the case of 3 beds)?
Thanks again for the help!
Dear Malcolm,
sorry forgot the echo for the output in the rent example
as how to add extra posts to display the filter is being added in the following line
//select the lowest two to display
$number_of_beds = array_slice( $number_of_beds, 0, 2 );
where 2 is the number of elements to consider, we are slicing the array by the first two elements if you increase this to 3 them you'll have three elements.
How are you saving the number of beds associated to each unit?
Thanks,
Ana
Thanks Ana,
The Beds are stored as a taxonomy of the 'units' post type.
I can get them out as 2 beds in my example, but for some reason your code simply returns '0 beds'...I can't figure out why?
Dear Malcolm,
In my first example I was using a custom field for saving the beds and not a taxonomy. Now the same example using a custom taxonomy, note that in the following example the taxonomy used is 'number_of_beds' so you should adjust this to your case.
$all_child_posts = types_child_posts('unit');
$number_of_beds = array();
foreach ($all_child_posts as $child_post) {
$number_of_beds[] = strip_tags(get_the_term_list( $child_post->ID, 'number_of_beds'));
}
//display only distinct values
$number_of_beds = array_unique( $number_of_beds );
//sort low to high
asort( $number_of_beds );
//select the lowest two to display
$number_of_beds = array_slice( $number_of_beds, 0, 2 );
$single_plural = count($number_of_beds);
if ( $single_plural == 1 ) {
$single_plural = $number_of_beds[0];
} else if ( $single_plural == 0 ) {
$number_of_beds[] = 0;
}
$output = implode( ' & ', $number_of_beds) . ' ' . _n( 'bed', 'beds', $single_plural );
echo $output;
Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Ana
Perfect Ana!
Thank you so much for your assistance 🙂