-1

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

calvincani
  • 193
  • 2
  • 11
  • 2
    `$venue['value'][0]->post_title;` – Markus Zeller Aug 06 '20 at 09:43
  • 2
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Epodax Aug 06 '20 at 09:45

1 Answers1

2

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.

Markus Zeller
  • 8,516
  • 2
  • 29
  • 35