HarleyCreative
HarleyCreative

Reputation: 303

Wordpress: Display content from a Custom Post Type within a regular Post

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:

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)

this diagram
(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

What I've Tried and Why It Didn't Work:

  1. 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)
  2. Custom Field

    • Entered ID (21) into custom field on post editor page.
    • Coded it directly into the post template:
      • Used WP_Query and had the ID in the array pull from the custom field.
    • Result: Nothing good happened.

Where I Keep Running Into Trouble:

The Optimal Solution:

Whether it's by using a shortcode, custom field, or even a new widget, it would be easiest if one could:

  1. 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
  2. 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.

Upvotes: 2

Views: 5610

Answers (3)

Pete
Pete

Reputation: 113

Add this loop after the normal loop...

<?php /* Display all the author's posts from the custom post type ('band') */ ?>
<?php
    $authorid = get_the_author_meta( ID, $userID );
           $args4=array('author'=>$authorid,'post_type'=>'band', 'numberposts'=> -1);
           $cquery4=new WP_Query($args4);
           if($cquery4->have_posts()):
               while($cquery4->have_posts()):
                   $cquery4->the_post();
           ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
               <p><?php the_title();?></p>
</a>
           <?
               endwhile;
                wp_reset_postdata();
           endif;
?>

Upvotes: 0

S_Pixselig
S_Pixselig

Reputation: 11

I knew you have found a solution for your problem, but for others i will give them an other solution:

You can query by ID, no problem. Look at these: http://www.advancedcustomfields.com/resources/field-types/relationship/ But you have to check in the custom field "return format" the box "Post IDs". Then it works perfectly well.

Sorry for my bad english ;) Cheers

Upvotes: 1

HarleyCreative
HarleyCreative

Reputation: 303

With some amazing help from Hobo, I was able to come up with a solution!

The main problem was with trying to query by 'ID'. No matter what we tried, it just never worked well. (It could be the way that the Advanced Custom Fields stored the ID field contents.)

What did work:

  1. Created a custom field for the post page in which to put the Band (custom post type) post name/slug into. (Custom field was named post-band-name and created with the Advanced Custom Fields plugin.)
  2. Placed the query code in the Post Template. (See code below)
  3. Done.

The Solution Code

Upvotes: 0

Related Questions