Skip Navigation

[Résolu] Get date of child post as set by post relationship in functions php

This support ticket is created Il y a 5 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 3 réponses, has 2 voix.

Last updated by Brad Tipper Il y a 5 années et 10 mois.

Auteur
Publications
#904166

I have a post type safety-check and a child post log-item. I'm writing a function that needs to do the following:

1) get the newest log-item of the current safety-check (in a view)
2) get the interval that has been set for the safety-check (this is an integer in number of weeks)
3) calculate the next date that a check is due

Here's what I have so far:

	function get_next_check(){

		$childargs = array(
			'post_type' => 'log-item',
			'posts_per_page' => 1,
			'orderby' => 'date',
			'order' => 'ASC'
		);
		$child_posts = types_child_posts('log-item', $childargs);

		foreach ($child_posts as $child_post) { 
			$last_check = get_the_date($child_post->ID);
		}

		// Get the check interval from safety-check
		$interval = types_get_field_meta_value( 'interval', $post_id );

		// Calculate date of next check
		$next_check = date(get_option('date_format'), strtotime($last_check." ".$interval." weeks"));

		// return $last_check;
		// return $interval;
		return $next_check;

	}
	add_shortcode( 'next_check', 'get_next_check' );

Here's what happens:

return $last_check;

gives me today's date

return $interval;

always gives me 2

return $next_check;

gives me really strange dates.

#904168

You can see that code in functions.php by the way.

<access removed>

#904270

To debug this code we need to know what $child_posts returns to you.
It should be an array of post obejcts.

You can check that by for example doing a var_dump of $child_posts, and if it's the correct data, then go further and check what $child_post->ID in the foreach outputs.
If that is the correct ID, then get_the_date($child_post->ID) should work and return the date of the post, this is a WordPress Function:
https://codex.wordpress.org/Function_Reference/get_the_date

To get a Custom Field value instead, could you try with a function like types_render_field or get_post_meta?
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://developer.wordpress.org/reference/functions/get_post_meta/
Remember to use wpcf- prefix for the Fields slugs if you use WordPress API get_post_meta().
What values are you receiving then?
It should be the value of the field stored for the post id you passed.

But all this should happen inside the foreach, since you want to do that for each post, correct?

The same is probably valid for your next variable.

All these data you want from the single posts, not from the "bulk".

Hence, after you get all posts with the types_child_posts function (or a get_posts), you have to foreach over those objects, and extract the data you want fro each of them.
Populate the variables, and then decide what to do with them.

If you require more help for custom code I can also reccomend the contractors:
https://toolset.com/contractors/

The only that you need to keep in mind in your code which is Toolset related, is that if you use WordPress API to get the fields (which I suggest, since our API is just a wrapper for the WordPress API), is to use "wpcf-" prefixes for all custom fields.
This is important as otherwise no data will be returned in get_post_meta() calls for example.

#904912

Thanks, that definitely pointed me in the right direction.

I ended up with the following- all works correctly:

// Next Check
// =============================================================================
function get_next_check(){

	// Get the check interval from safety-check
	$interval = get_post_meta( get_the_ID(), 'wpcf-interval', true);

	$childargs = array(
		'post_type' => 'log-item',
		'posts_per_page' => 1,
		'orderby' => 'date',
		'order' => 'ASC'
	);

	$child_posts = types_child_posts('log-item', $childargs);

	if (!empty($child_posts)){

		foreach ($child_posts as $child_post) {
			$last_check = $child_post->post_date;
		}

		// Calculate date of next check
		$next_check = date(get_option('date_format'), strtotime($last_check." ".$interval." weeks"));

		return $next_check;

	} else {

		return "<strong>Not checked</strong>";

	}

}
add_shortcode( 'next_check', 'get_next_check' );
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.