I feel like there is a really simple solution to this problem. However, after trying to solve it for about 3 hours unsuccessfully, I humbly come to you.
The Basics:
- A custom post type, " band ", has been created, and has several custom fields (which were created through the Advanced Custom Fields plugin.)
The Question:
How would I get and display the contents (specifically custom field data) of a specific band entry (using its ID or title or slug) inside of a regular post? (see diagram below)
(source: thisnewband.com)
.
Methods to Trigger the Display of the Custom Post Type:
We have to provide the ID/title/slug so that it knows what band post's content to display
- Shortcode (such as
[band id="21"]
) (added inside post content) - Custom Field (custom field name
band-id
where you can input the ID of the band)
What I've Tried and Why It Didn't Work:
Shortcode
- `[band id="21"] inserted in post editor field
- Used
WP_Query
to query post with type=band and ID="21". - Code located in
functions.php
- Result: It would echo static text but would not display any post-specific content (Band Name, etc.). Also would not pull post-specific custom field data.
- (also tried
query_post
with no luck)
Custom Field
- Entered ID (21) into custom field on post editor page.
- Coded it directly into the post template:
- Used
WP_Query
and had theID
in the array pull from the custom field.
- Used
- Result: Nothing good happened.
Where I Keep Running Into Trouble:
- It's hard to pull the custom field data from the custom post type while inside an actual post
The Optimal Solution:
Whether it's by using a shortcode, custom field, or even a new widget, it would be easiest if one could:
- Create a PHP template with the code for just how the single Band content is supposed to display. (Including the loop). Example name: band-block.php
- Use
get_template_part('band-block');
to echo this code (either in Post Template or Shortcode via functions.php)
Thanks for your help! Let me know if you'd like to see any of my code.