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.
function create_posttype() { register_post_type( 'room', array( 'labels' => array( 'name' => __('Conference rooms'), 'singular_name' => __('Conference room') ), 'menu_icon' => 'dashicons-building', 'menu_position' => 5, 'public' => true, 'has_archive' => false, 'publicly_queryable' => false, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => false, 'capability_type' => 'post', 'supports' => array('title') ) ); }
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:
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,…)
Field Label: Price per night,
Field Name: price_per_night,
Field Type: Number,
Default Value: 5
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.
<?php $featured_post = get_field('featured_post'); if( $featured_post ): ?> <h3><?php echo esc_html( $featured_post->post_title ); ?></h3> <?php endif; ?>
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:
$featured_post = get_field('room', 'room=room-01'); $featured_post = get_field('room_name', 'room=room-01'); $featured_post = get_field('room_name', 'room');
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…
<form> <label for ="room">Select a room:</label> <select name="room" id="room"> <?php $args = array( 'post_type' => 'room', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC' ); $the_query = new WP_Query($args); $sobe = get_posts($args); foreach ($sobe as $post){ setup_postdata($post); $room_title = get_field('room_name'); ?> <option value=" <?php echo $room_title ?> " > <?php echo $room_title ?> </option> <?php } wp_reset_postdata(); ?> </select> </br> </br> </form>