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'>—</span>";
}
return $last_check_date;
}
add_shortcode( 'last_check', 'get_last_check' );
Any idea why that would be? Cheers.
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
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'.
);
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.
So how do I get the latest post based on post_date?
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.
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
OK, that looks like it should work- thanks. Where do I submit a feature request?
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?
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
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.