Skip to content
Advertisement

Displaying a dropdown menu of custom post titles with ACF (Advanced Custom Fields) in WordPress

I would like to display a list of custom post titles with the help of Advanced Custom Fields.

First off, I have created a custom post type.

JavaScript

Then I have created a few “posts” inside this custom post type ie. Room 01, Room 02, etc. Next, I have created a new Custom Field for each room and set the Location Rule to point to the appropriate room, using ACF.

The fields for each room include:

  1. Field Label: Room name,

    Field Name: room_name,

    Field Type: Text,

    Default Value: Room 01 (each room has a different value ie. Room 02, Room 03,…)

  2. Field Label: Price per night,

    Field Name: price_per_night,

    Field Type: Number,

    Default Value: 5

  3. Field Label: Post object,

    Field Name: post_object,

    Field Type: Post Object,

    Return Format: Post Object

After this, I returned to the custom post type (room) and edited every room (Room 01, etc), so the Post object points to the appropriate page (in this case a page named Reservation with its custom template).

That being done, I opened up the custom template PHP file (which Reservation is using) and added this code, which I copied from the ACF documentation.

JavaScript

Not knowing what I have to call in the get_field section I searched for a room in my WP, which pointed me to ?room=room-01. Looking up get_field() I assume I have to point to the field in which I want to be displayed (argument 1) and the location where the field is located (argument 2).

Now, no matter what I try, I can’t get the title drop-down menu to appear…

A few things I have tried:

JavaScript

If I omit the if statement I get this warning/notice:

Notice: Trying to get property ‘post_title’ of non-object

To conclude, I can’t seem to get it to work. Any help, pointers, advice, and explanations would be most welcome.

If I can provide any additional information, feel free to ask.

Advertisement

Answer

After a lot of searching I have found some related questions and after some work I came up with this…

It’s not what I wanted exactly but it works…

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement