Skip Navigation

[Resolved] Child post fields are shown with types_child_fields but not with get_posts

This thread is resolved. Here is a description of the problem and solution.

Problem: I have some custom code that uses types_child_posts to get a list of child posts. Then I am able to use the $post->fields['field-slug'] syntax to display custom post fields from the child post. However, if I use the native WordPress get_posts function to get the child posts, I am not able to use the same $post->fields['field-slug'] syntax to get information about a custom field.

Solution: The native WordPress get_posts function does not return information about custom fields, so you must use get_post_meta to retrieve custom field information about each post returned by get_posts.

$date = get_post_meta(get_the_ID(), 'wpcf-date', true);
echo date('l jS \of F Y h:i:s A', $date);

Relevant Documentation:
https://developer.wordpress.org/reference/functions/get_posts/
https://developer.wordpress.org/reference/functions/get_post_meta/

This support ticket is created 7 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by lemoigneL 7 years ago.

Assisted by: Christian Cox.

Author
Posts
#584859

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!

#584945

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'];
}
#585071

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')

#585523

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

#585700

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!
*****