Wordpress PHP Array how do I get the value
Get the solution ↓↓↓I have Wordpress Site and I am using Advanced custom fields plugin. So I created an event post type and one of the fields is a relationship field that is linked to the venue where the event will take place. The field outputs an object and I have the following code:
<?php
$venue = get_field_object('event_venue');
print("<pre>".print_r($venue,true)."</pre>");
?>
that prints out the following
Array
(
[ID] => 437
[key] => field_5f2a55020b621
[label] => Venue
[name] => event_venue
[prefix] => acf
[type] => relationship
[value] => Array
(
[0] => WP_Post Object
(
[ID] => 216
[post_author] => 1
[post_date] => 2020-06-19 09:53:06
[post_date_gmt] => 2020-06-19 09:53:06
[post_content] =>
[post_title] => test venue
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => test-venue
[to_ping] =>
[pinged] =>
[post_modified] => 2020-06-19 09:53:06
[post_modified_gmt] => 2020-06-19 09:53:06
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost/threedir/?post_type=venue&p=216
[menu_order] => 0
[post_type] => venue
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
[menu_order] => 7
[instructions] =>
[required] => 1
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => 203
[wrapper] => Array
(
[width] =>
[class] =>
[id] =>
)
[post_type] => Array
(
[0] => venue
)
[taxonomy] =>
[filters] => Array
(
[0] => search
)
[elements] =>
[min] =>
[max] =>
[return_format] => object
[_name] => event_venue
[_valid] => 1
)
How do I print just the post title that is inside the value array?
Because if I do a
print($venue['value']["post_title"]);
I get a
Notice: Undefined index: post_title
Answer
Solution:
You can access it with
$venue['value'][0]->post_title;
Because$venue['value']
is an array, which first element contains an object. Objects properties are accessed via arrow notation.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: failed to create image decoder with message 'unimplemented'
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.