Support

Account

Home Forums General Issues Show Custom Field results from one post type in template of another

Solved

Show Custom Field results from one post type in template of another

  • Will do my best to explain what I am trying to do here.

    I have two custom post types – one called ARTIST and one called RELEASE.
    Think of a record/music label here.

    The ARTIST post type contains the artist details like their name, photo, biography, web links etc. I can display all of this in an ARTIST template without any problems.

    In the RELEASE post type, I have fields for the release like album title, artist name, track listing, link to itunes to purchase etc.

    Again I can display this in RELEASE template easily enough.

    My problem? I want to display all of the releases by any given artist on their respective ARTIST page. I have no idea how to do this.

    For example, how would I show all of the releases by METALLICA on the METALLICA ARTIST page? Any suggestions or things to try appreciated.

  • I just had a similar problem where I wanted to pass data from one page template to another. I connected the 2 pages via drop down menu of all the pages on the site. In your case, your drop down menu would accommodate posts instead of pages.

    If I understand your problem, you want to avoid adding duplicate info on your ARTIST post because it’s already been entered on the RELEASES post. So you could enter the data on the RELEASES post and then connect to the RELEASES post by using a drop down menu on the ARTIST post.

    Here’s what worked for me– thanks to Jamie and Elliot for showing me how to do this.

    and here it is copied from that bulletin board page:

    Get field data from another page via drop down menu:

    Page A (master page)
    Page B (sub-page): Linked to Page A via drop-down menu

    Create a post object field on Page B’s ACF called ‘select_page’

    In order for Page B to access field data from Page A:

    Add something like this in Page B’s php code:

    <? $pages = get_field('select_page');
        get_field('content_from_page_A', $pages->ID);
    ?>

    You can have as many get_field(‘content_from_page_A’, $pages->ID) as you need where ‘content_from_page_A’ is the name of any field on the RELEASES template and $pages->ID gets the page ID number of the RELEASES page as defined by the drop down menu of pages (or posts in your case).

    $pages->ID is what hooks the two templates together. If we weren’t using a variable, the syntax would look like this:

    get_field(‘content_from_RELEASES_page’, ID of RELEASES page)

  • I feel like I am over-complicating things as I don’t understand your post, though thankful for you trying!

    How about this…

    CUSTOM POST TYPE: RELEASE
    VALUES: Album cover art, Album title, Album Artist, Album track listing

    How do I display ALL of the releases by one artist.

    So on my ARTIST page for Metallica, I would have their 10 albums.
    On my artist page for Coldplay, I would have their 5 albums etc.

    I don’t understand how to link a RELEASE to an ARTIST and then show only those RELEASES for any given ARTIST.

  • Since you’re using posts instead of pages, there needs to be a way to filter only the posts that you want (In this case, Band releases). The post object is probably the way to go. Also look up post objects in the WordPress codex to see what data type you can access.

    Sorry I can’t be of more help here.

  • Hi @lowercase

    If you are using the post_object to select an ‘artist’ for each ‘release’, then you can simply query the posts to find all releases from 1 artist by following this tutorial:
    http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/

    Thanks
    E

  • Hi @elliot

    I still don’t really follow how that will work. In the example, that requires you put in a key value of ‘Melbourne’. That is great if you only have one value called ‘Melbourne’, but I have 30 different artists.

    How can I tell which artist page I am currently on before I show the value for that particular artist without setting up 30 different templates?

    Does that make sense?

    If I am on the Metallica page, I ONLY want to show Metallica releases. With 30 artists, do I need to set up 30 templates to call the specific artist value on each one?

  • Hi @lowercase

    If you are viewing the post of ‘Metallica’, then the current post id can be found like so:

    
    $post_id = get_the_ID();
    

    This is the value you should use to query the posts with!
    Change ‘Melbourne’ to $post_id

    Thanks
    E

  • Thanks @elliot but still struggling with this all day again.

    I think I am way off track here and am now even more confused.

    Do I need to link the posts in any way whatsoever? Do I need to add any sort of custom filed to the ARTIST page to link it to RELEASES?

    I am positive this will take 5 lines of code, but it has become so complicated. Is what I am doing at all unusual or am not explaining it well enough?

    Maybe another approach using books.

    Two custom post types…

    BOOK.
    BOOK AUTHOR.

    ARCHIVE PAGEs for both BOOK and BOOK AUTHOR exist.

    I want to…

    LIST BOOK on BOOK AUTHOR PAGE. Simple right?

    So if i have 10 books by Stephen KING. I want to list them all (and only those by him) on the STEPHEN KING page.

  • This is the code I use to display ALL of the releases. It works great.

    I need to somehow adapt this query to show only the Releases of a specific artist on their Artist page. Thought it might help…

    <?php
         $args=array(
         'post_type' => 'book',
         'post_status' => 'publish',
         'posts_per_page' => 12,
         'caller_get_posts'=> 1,
         'orderby'=> 'date',
         'order' => 'DESC'
         );
         $my_query = null;
         $my_query = new WP_Query($args);
         if( $my_query->have_posts() ) {
         echo '';
         $i = 0;
         while ($my_query->have_posts()) : $my_query->the_post();
         if($i % 6 == 0) { ?>
    <div class="row">    
    <?php
    }
    ?>
    
    <img src="<?php the_field('cover_image'); ?>" /><br />
    <?php the_field('author_name'); ?><br />
    <?php the_title(); ?><br />
    
    </div>
    
    <?php    
    if($i % 6 == 0) { ?> 
    
    <?php
    }
    
    $i++;
    endwhile;
     } 
    wp_reset_query();
    ?>
  • Assuming the artist’s name is the also the title for your “artist” post type, match it with the “artist_name” custom field in your “release” post type.

    On your “artist” single post template:

    // args
    $args = array(
    	'numberposts'=> -1,
    	'post_type'=> 'release',
    	'meta_key' => 'artist_name',
    	'meta_value' => the_title()
    );
    
  • Hi @andisaleh

    Thanks for your reply, but I am still really struggling with this. Not sure where that code you provided should go. Where would it fit in relation to my full query/code here…

    <?php
         $args=array(
         'post_type' => 'book',
         'post_status' => 'publish',
         'posts_per_page' => 12,
         'caller_get_posts'=> 1,
         'orderby'=> 'date',
         'order' => 'DESC'
         );
         $my_query = null;
         $my_query = new WP_Query($args);
         if( $my_query->have_posts() ) {
         echo '';
         $i = 0;
         while ($my_query->have_posts()) : $my_query->the_post();
         if($i % 6 == 0) { ?>
    <div class="row">    
    <?php
    }
    ?>
    
    <img src="<?php the_field('cover_image'); ?>" /><br />
    <?php the_field('author_name'); ?><br />
    <?php the_title(); ?><br />
    
    </div>
    
    <?php    
    if($i % 6 == 0) { ?> 
    
    <?php
    }
    
    $i++;
    endwhile;
     } 
    wp_reset_query();
    ?>

    Any help appreciated.

  • Hi @andisaleh
    Using your code I can now output the artist title on the page, but it does nothing to bring the releases in. Any thoughts?

  • Hi @elliot
    Again, I appreciate the response, but I just don’t see how this helps what I am trying to do. I must have read that page you keep link to 30 times and don’t see how it is relevant.
    I shall chalk this one down to me being stupid and hope someone else can offer some more in depth assistance.
    I just have always assumed this to be a really common use of custom post types, but starting to feel it is beyond things here.

    Thanks again.

  • Here’s a step-by-step.
    1. Find a way to get the artist’s name. It’s stored as the post’s title. Great. We can grab it with the_title().
    2. Find where the artist is declared in the ‘release’ CPT. It’s inside a custom field named album_artist. Custom fields are stored inside meta keys.
    3. Construct a query with these values and spit out the results. You get:

    <?php
    $args = array(
      'numberposts'=> -1,           // Fetch all posts...
      'post_type'=> 'release',      // from the 'release' CPT...
      'meta_key' => 'album_artist', // which inside this meta key...
      'meta_value' => the_title()   // has this meta value.
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) :
      echo '<ul>';
      while ($my_query->have_posts()) : $my_query->the_post();
    ?>
    
    <li><?php the_field('album_title') ?></li>
    
    <?php 
    endwhile; echo '</ul>';
    endif; wp_reset_query(); ?>

    Hope that helped.

  • Hi @andisaleh

    Thanks for the explanation, but that now outputs ALL of the releases. Which makes it no different to my original post/query which outputs everything.

    How do I get it to recognize what artist page i am on and then ONLY output releases from that artists. Right now it is just bringing in every release from every artist on every page.

    Does that make sense?

  • Hi @andisaleh

    The meta_value should be the value which is saved, which in this case is the ID, not the title.

    So, you should change the_title() to get_the_ID()

    This will then query posts of post_type release where the selected artist is the current artist being viewed.

    Thanks
    E

  • Hi @andisaleh and @elliot

    I feel this is getting closer but still can’t work out why such a simple task of grabbing one value from another post and matching it to current post is so hard. I am going crazy! haha.

    New query is now this, but unfortunately it returns nothing.

    <?php
    $args = array(
      'numberposts'=> -1,           // Fetch all posts...
      'post_type'=> 'release',      // from the 'release' CPT...
      'meta_key' => 'artist_name', // which inside this meta key...
      'meta_value' => get_the_ID(),   // has this meta value.
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) :
      echo '<ul>';
      while ($my_query->have_posts()) : $my_query->the_post();
    ?>
    <li>
    <img src="<?php the_field('cover_image'); ?>" />
    <?php the_field('artist_name'); ?>
    <?php the_title(); ?>
    </li>
    
    <?php 
    endwhile; echo '</ul>';
    endif; wp_reset_query(); ?>

    Failing this – do either of you know any quick places I can go to pay someone to do this? I just don’t have any contacts in that regard.

  • We really need to know how you structure your custom fields in the release CPT. I simply assumed in the release CPT you have a custom field named album_artist which is a text field that holds the artist’s name. If so, then it was correct to match it with the title of the artist CPT which is also their name.

    Release CPT
    Post title: Death Magnetic
    Album title (album_title, text field): Death Magnetic
    Album artist (album_artist, text field): Metallica

    Artist CPT
    Post title (the_title()): Metallica

    So, meta_key => album_artist = the_title().

    Really, you just need to find what values you can match between your CPT and you’re all set.

  • I just realized the_title() outputs the value. *facepalm* Try get_the_title() instead. 🙂

  • Hi @andisaleh

    That did the trick! get_the_title() was the problem.
    I cannot thank you enough – this has consumed days of my life this week, and I am very appreciative.

    thanks to everyone else who offered asssitance.

Viewing 22 posts - 1 through 22 (of 22 total)

The topic ‘Show Custom Field results from one post type in template of another’ is closed to new replies.