Skip Navigation

[Resolved] types_child_posts – Returning two values, first and last

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 9 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

This topic contains 7 replies, has 2 voices.

Last updated by malcolmG 9 years, 9 months ago.

Assisted by: Ana.

Author
Posts
#228059

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 &nbsp;&nbsp;&nbsp; ';
									}
								}

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="&#xe067;" 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??

#228187

Ana
Supporter

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="&#xe067;" 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

#228205

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?

#228209

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!

#228213

Ana
Supporter

Dear Malcolm,

sorry forgot the echo for the output in the rent example

echo $output;

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

#228215

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?

#228220

Ana
Supporter

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

#228224

Perfect Ana!

Thank you so much for your assistance 🙂

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.