Skip Navigation

[Resolved] types_child_posts not accepting arguments

This support ticket is created 6 years, 5 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 12 replies, has 2 voices.

Last updated by Luo Yang 6 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#920221

I am trying to: Write a custom shortcode w- query to get the date of newest child post- as defined by a post relationship.

However, types_child_posts doesn't seem to be accepting the arguments being given to it, because it's displaying all the child posts, not just one. The shortcode function is this:

function get_last_check(){
	$childargs = array(
		'post_type' => 'log-item',
		'posts_per_page' => 1,
		'orderby' => 'post_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;
			$last_check_date .= date_i18n( get_option( 'date_format' ), strtotime( $last_check ) );
		}
	} else {
		$last_check_date = "<span class='status status-not-checked'>&#8212;</span>";
	}
	return $last_check_date;
}
add_shortcode( 'last_check', 'get_last_check' );

Any idea why that would be? Cheers.

#920655

Hello,

Are you using the latest version of Types plugin and the new post type relationship?
If it is, I suggest you try the new Types API function: toolset_get_related_posts
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

It can retrieve the related posts, and you can setup the argument "$args"

array Additional query arguments. Accepted arguments:
meta_key, meta_value and meta_compare: Works exactly like in WP_Query. Only limited values are supported for meta_compare at the moment (‘=’|’LIKE’).

More help:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

#920663

Thanks, that looks like it will work. One problem though, the last two parameters (orderby and order) don't appear to work. Changing from ascending to descending has no effect, and neither does the order by. Posts stay in the same order no matter what I put for those values. Do you know why would that be?

Here's my code:

	$child_posts = toolset_get_related_posts(
		get_the_ID(), //Post to query by.
		'safety-check-log-item',  //Slug of the relationship to query by
		'parent', //Name of the element role to query by.
		10, //Maximum number of returned results
		0, //Result offset
		array(), //Additional query arguments
		'post_object', //Determines return type
		'child', // which posts from the relationship should be returned
		'post_date', //orderby: null, 'title', 'meta_value', 'meta_value_num'
		'DESC' // $order 'ASC' or 'DESC'.
	);
#920706

The argument "orderby" accepts only below values:
null, ‘title’, ‘meta_value’, ‘meta_value_num’,
It does not accept value "post_date", so your codes won't work, see our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
$orderby – null|string Determine how the results will be ordered. Accepted values: null, ‘title’, ‘meta_value’, ‘meta_value_num’. If the latter two are used, there also needs to be a ‘meta_key’ argument in $args. Passing null means no ordering.

if you agree we can take it as a feature request.

#920709

So how do I get the latest post based on post_date?

#920717

I suggest you create a post view, order the result by the post publish date
filter by the post type relationship "safety-check-log-item"
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/displaying-related-child-posts/#3-creating-a-view-that-lists-the-children-of-the-parent-post

and in the view's loop, display the post publish date
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-date

Then use the Views shortcode [wpv-view] to replace your custom shortcode [last_check]
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-view

#920787

That won't work for me sorry. The whole point of having it in functions.php is so that I can run some calculations on the dates to do a few things that aren't possible in Toolset.

If it can be ordered by title why on earth can't it be ordered by post_date? Seems really strange to me. Cheers.

#920795

Yes, it is very strange, it is in the document for now.

If you want to order the results by post_date in function toolset_get_related_posts(), it will need a feature request, see my answer above:
https://toolset.com/forums/topic/types_child_posts-not-accepting-arguments/#post-920706

As a workaround, you can setup a view, query the related posts, order the results by post_date, and you can get the Views results with Views API function using PHP codes, for example get_view_query_results():
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results

#920813

OK, that looks like it should work- thanks. Where do I submit a feature request?

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/function-toolset_get_related_posts-not-accepting-arguments/

#921045

OK, I have split this thread, and setup the new thread as a feature request:
https://toolset.com/forums/topic/function-toolset_get_related_posts-not-accepting-arguments/#post-921044

#921148
2018-07-05 20_00_20-.png
2018-07-05 20_03_02-.png
2018-07-05 20_03_55-.png

Sweet, cheers. I've got the view set up, and it's displaying the correct date (using [wpv-post-date] ) However, when I use

$child_posts = get_view_query_results( 516 );

in functions.php I just get an empty array. I assume I need to pull the ID of the current post into that somehow?

#921190

In your screenshot:
hidden link
There is a filter:
Select posts in Any relationship ... to the current post in the loop

So you will nee to run the function get_view_query_results( 516 ) in the parent view's loop.

To debug this problem, you can remove the that filter and test again.

If the problem still persists, please provide a test site with the same problem, also point out the problem page URL and where I can edit your PHP codes, I need a live website to test and debug

#921508
login.JPG

The credentials you provided above is not valid, I get only a login window, see screenshot login.JPG.

So it is not possible to debug the codes in your website

As I mentioned above, it is very simple to debug your PHP codes, you just need to remove the filter in your screenshot:
Select posts in Any relationship ... to the current post in the loop
hidden link

And test again.

I have tested the function get_view_query_results in my localhost, it works fine.

For example, create a custom shortcode, add below codes into your theme file:

add_shortcode('my-func', function($atts, $content){
	
	$child_posts = get_view_query_results( 123456 );
	var_dump($child_posts);
});

Please replace 123456 with your view's ID

Put above shortcode into a wordpress page, and test the page in front-end.