Reputation: 810
I have a custom post type, event, that has a SELECT field that grabs the Post Object of another custom post type, listing. So we have an event and we want to select a business listing that it's attached to it so we can display the business's contact information (phone, address, etc).
functions.php
function register_post_types() {
register_post_type( 'swfl_listing', array(
'labels' => array(
'name' => __( 'Listings' ),
'singular_name' => __( 'Listing' )
),
'rewrite' => array(
'slug' => 'destinations/listings',
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'capability_type' => 'post',
'has_archive' => true,
'supports' => array( 'title', 'thumbnail', 'editor', 'revisions', 'author', 'comments' ),
'menu_icon' => 'dashicons-businessman',
) );
register_post_type( 'swfl_event', array(
'labels' => array(
'name' => 'Events',
'singular_name' => 'Event',
),
'rewrite' => array(
'slug' => 'discover/events',
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'capability_type' => 'post',
'has_archive' => true,
'supports' => array( 'title', 'thumbnail', 'editor', 'revisions', 'comments' ),
'menu_icon' => 'dashicons-calendar-alt',
) );
}
function add_to_context( $context ) {
...
$context['events'] = Timber::get_posts( array(
'post_type' => 'swfl_event',
) );
...
}
I searched and found this line of code that allows me to grab the information on the Singular page listing_sponsoring_event
being the custom field.
single.php & archive.php
...
$context['listing'] = new TimberPost( $post->listing_sponsoring_event );
...
Here is working code on the singular page
single-swfl_event.twig
...
<strong>Where: </strong> {{ listing.title }} <br/>
<strong>Price: </strong> {{ post.get_field('event_price') }} <br/>
...
But the same code does not work if I put it into archive.php. It just shows the last post information in the loop.
archive-swfl_event.twig
...
{% for post in posts %}
...
<strong>Where: </strong> {{ listing.title }} <br/>
<strong>Price: </strong> {{ post.get_field('event_price') }} <br/>
...
{% endfor %}
...
Edit: I haven't been able to figure out anything using Timber/Twig (nor a response), but I'm leaving this question open just in case it gets figured out eventually. For now I am using a function with PHP to grab the information. It's not a pretty solution.
Hopefully Timber will eventually be updated to support SELECT and WP Post objects more.
Upvotes: 0
Views: 1106
Reputation: 810
So my problem was really just with working with WP Post Objects and how to get it's information. For any of you who also have problems:
Instead of {{ post.custom_field_name }}
, access the object information with {{ post.get_field('custom_field_name') }}
. {{ post.custom_field_name }}
only gives me the ID of the Object.
For troubleshooting, call
{{ post.get_field('custom_field_name')|print_r }}
for a list of data you can retrieve.
Now I can grab the data I want with {{ post.get_field('custom_field_name').post_title }}
.
Upvotes: 2