Home › Forums › Front-end Issues › Post Object with single value
So, I have created CF, type: post object, with Select multiple values = OFF. Added value in wp-admin without any problem. In page template, I use get_field_objects to get values. However, $field[‘value’] doesn’t return anything.
If I turn Select multiple values = ON, then $field[‘value’] shows correct value.
Here is the code. Did I do something wrong?
$fields = get_field_objects( get_the_ID() );
if ( $fields ) {
/* sorting */
$order = array();
foreach ( $fields as $key => $field ) :
$order[ $key ] = $field[ 'menu_order' ];
endforeach;
array_multisort( $order, SORT_ASC, $fields );
/** showing result on front-end **/
foreach ( $fields as $field ) {
echo '<h6>'.$field['label'].'</h6>';
$field_values = $field['value'];
if ( is_array( $field_values ) ) {
echo '<ul>';
$real_values = get_field( $field['name'] );
foreach ( $real_values as $post ) {
setup_postdata( $post );
echo '<li>'.get_the_title().'</li>';
}
echo '</ul>';
wp_reset_postdata();
}
else {
echo '<ul><li>'.$field_values.'</li></ul>';
}
}
}
What is being shown here? anything?
echo '<ul><li>'.$field_values.'</li></ul>';
You are returning a post object, a post object is not an array and is not a single value.
My expectation here would be that you’d see something like
Recoverable fatal error: Object of class WP_Post could not be converted to string in .../template-parts/post/content.php on line 12
No, there is nothing shown there. The worst thing is no error returns in front-end. Just a blank space. It cuts off all results, WordPress code after that. For example, no more footer, even when I view source code, there are still close body, html tags. Something likes break in the loop.
In the server log, there is error recorded anyway:
Stack trace:
#0 /home/<username>/domains/<the-domain-name>/public_html/wp-includes/class-wp-hook.php(287): newfunction_loop('')
#1 /home/<username>/domains/<the-domain-name>/public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters('', Array)
#2 /home/<username>/domains/<the-domain-name>/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array)
#3 /home/<username>/domains/<the-domain-name>/public_html/wp-content/themes/genesis/lib/framework.php(66): do_action('genesis_loop')
#4 /home/<username>/domains/<the-domain-name>/public_html/wp-content/themes/theme20/page-abc-template.php(73): genesis()
#5 /home/<username>/domains/<the-domain-name>/public_html/wp-includes/template-loader.php(106): include('/home/<username>/dom...')
#6 /home/<username>/domains/<the-domain-name>/public_html/wp-blog-header.php(19): require_once('/home/<username>/dom...')
#7 /home/<username>/domains/<the-domain-name>/public_html/index.php(17): require('/home/<username>/dom...')
# in /home/<username>/domains/<the-domain-name>/public_html/wp-content/themes/theme20/page-abc-template.php on line 64
if you see nothing then you are seeing a partial white screen, a fatal error is causing execution to stop. Turn on debugging https://codex.wordpress.org/WP_DEBUG
Now, I see the same error as you posted above. However, I am not sure why it runs the “else” there?
what you need to do is detect this return value
if (is_a($field_values, 'WP_Post')) {
// return is a single post object
// convert it to an array for use below
$field_values = array($field_values);
}
if ( is_array( $field_values ) ) {
The topic ‘Post Object with single value’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.