Hi,
I'm trying to display relation child post in a custom query.
I have a post called "artist" and it display child post called "artworks"
When I'm using types_child_posts('artworks'), then It works well.
I can display the titling of each children as well as custom fileds attached to them (date)
Simple Example:
$child_posts = types_child_posts('artworks');
foreach ($child_posts as $child_post) {
echo $child_post->post_title;
echo $child_post->fields['date'];
}
Now I'd like to add a custom query to change order of the artworks, so I've used:
$childargs = array(
'post_type' => 'artworks',
'numberposts' => -1,
//'meta_key' => 'wpcf-date',
//'orderby' => 'meta_value_num',
//'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_artiste_id', 'value'=>get_the_ID())) );
$child_posts = get_posts($childargs);
foreach ($child_posts as $child_post) {
echo $child_post->post_title;
echo $child_post->fields['date'];
}
I'm expeting to have the same result and then order them with the wpcf-date field.
All titlings are displaying correctly
However, with this method, I can't render the filed date : $child_post->fields['date']
It gives a php error
: Illegal string offset 'date' in ...
: Uninitialized string offset: 0 in ...
Any ideas would be really helpful... I've spend the day trying out to figure out a solution, without any success....
Thanks in advance!
Hi, have you tried inspecting the $child_post variable? What do you see on screen if you make the change below?
foreach ($child_posts as $child_post) {
echo $child_post->post_title;
print_r($child_post, true);
//echo $child_post->fields['date'];
}
I receive the object but without the fields associated to the post type :
WP_Post Object
(
[ID] => 8483
[post_author] => 1
[post_date] => 1994-10-30 14:51:33
[post_date_gmt] => 1994-10-30 13:51:33
[post_content] =>
[post_title] => épurateur éthique
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => epurateur-ethique
[to_ping] =>
[pinged] =>
[post_modified] => 2017-11-01 13:17:07
[post_modified_gmt] => 2017-11-01 12:17:07
[post_content_filtered] =>
[post_parent] => 0
[guid] => <em><u>hidden link</u></em>
[menu_order] => 11
[post_type] => oeuvre
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
When I use :
$child_post = types_child_posts('oeuvre')
instead of the custom query, I have the full object that I need :
WP_Post Object
(
[ID] => 8483
[post_author] => 1
[post_date] => 1994-10-30 14:51:33
[post_date_gmt] => 1994-10-30 13:51:33
[post_content] =>
[post_title] => épurateur éthique
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => epurateur-ethique
[to_ping] =>
[pinged] =>
[post_modified] => 2017-11-01 13:17:07
[post_modified_gmt] => 2017-11-01 12:17:07
[post_content_filtered] =>
[post_parent] => 0
[guid] => <em><u>hidden link</u></em>
[menu_order] => 11
[post_type] => oeuvre
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[fields] => Array
(
[image] => <em><u>hidden link</u></em>
[date] => 1994
[taille] => 66x33x548cm
[photo] =>
[autre] =>
[autres-images] => Array
(
[0] =>
)
)
[_wpcf_post_terms] => Array
(
[0] => 44
[1] => 110
[2] => 40
)
[_wpcf_post_template] =>
[_wpcf_post_views_template] =>
)
(My posttype called "artworks" in the first message is actually 'oeuvre')
Okay I see, the difference is that when you use the types_child_posts function Types automatically adds those fields into the post object. But when you use the native WP get_posts function, those fields are not added. This is the default WordPress behavior. You would have to use the get_post_meta function to get the custom field values. Types custom fields are stored in the database using the "wpcf-" slug prefix, so the code might look something like this:
...
echo $child_post->post_title;
$date = get_post_meta(get_the_ID(), 'wpcf-date', true);
...
If the 'date' field is just a string like "January 1, 2018", you can echo $date. If it's a date field, you have to format a date using the timestamp value stored in the database.
echo date('l jS \of F Y h:i:s A', $date);
More information about PHP date formatting here:
hidden link
Hi Christian,
Thanks a lot for the solution, that help me a lot !
I retrieve only text strings, and images, so I've used :
$myvar = get_post_meta($child_post->ID, 'wpcf-fieldnametoretrieve', true);
and to retrieve a loop of repeting fields (in an array)
$myvar = get_post_meta($child_post->ID, 'wpcf-fieldnametoretrieve', false);
That works perfectly!
*****