Title: Help needed using Magic Fields &#039;Related Type&#039;
Last modified: August 19, 2016

---

# Help needed using Magic Fields 'Related Type'

 *  [matstillo](https://wordpress.org/support/users/matstillo/)
 * (@matstillo)
 * [15 years ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/)
 * OK, I’m sure this is really basic, but I’m having a problem using the Related
   Type field in Magic Fields. I just want to display the title of the related post
   I have chosen, but I can only seem to get it to display a Magic Fields custom
   field.
 * `<?php echo get('related-post-title', 1, 1, false, get('my-related-field')); ?
   >`
 * my-related-field is the name of my related type field. related-post-title is 
   where I’m trying to retrieve the title of the related post. I’ve tried the_title,
   get_the_title, but this is obviously not the right way. I can get this to work
   if I reference a custom field, so i could always just duplicate the post title
   in a specially created custom field, but it seems a bit pointless if there is
   just a neat way to retrieve the title.
 * Can someone point out the blindingly obvious to me please?!
 * Fantastic plugin, by the way. I was blown away by Flutter, but this just takes
   it to another level. Nice work!

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [hunk](https://wordpress.org/support/users/hunk/)
 * (@hunk)
 * [15 years ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060366)
 * ok what return get(‘my-related-field’) ? return the correct post_id ?
 *  Thread Starter [matstillo](https://wordpress.org/support/users/matstillo/)
 * (@matstillo)
 * [15 years ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060393)
 * Yes, that works to return the post ID, but I want the post title instead.
 *  [hunk](https://wordpress.org/support/users/hunk/)
 * (@hunk)
 * [15 years ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060433)
 * if you set post->ID in get for related-post-title, return the correct value?
 * <?php echo get(‘related-post-title’, 1, 1, false, 123)); ?>
 *  Thread Starter [matstillo](https://wordpress.org/support/users/matstillo/)
 * (@matstillo)
 * [15 years ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060559)
 * Got there in the end!
 * As long as I was able to return the ID for the content my related type field 
   was selecting, I could then use the two WordPress functions to just return the
   Title & Permalink that I needed
 *     ```
       <?php $related_content = get('my-related-field'); ?>
   
       <?php echo get_permalink($related_content); ?>
   
       <?php echo get_the_title($related_content); ?>
       ```
   
 * If anyone spots any fatal flaws in my plan here, please let me know!
 * PS [@hunk](https://wordpress.org/support/users/hunk/), I pestered you directly
   via email on this earlier, via the magic fields website. I didn’t realise it 
   was you trying to help me here!
 *  Thread Starter [matstillo](https://wordpress.org/support/users/matstillo/)
 * (@matstillo)
 * [15 years ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060560)
 * Have also just got this to work to retrieve the featured image of the related
   content, if it helps anyone out!?
 * `<?php echo get_the_post_thumbnail($related_content, 'my-custom-thumbnail'); ?
   >`
 *  [knijia](https://wordpress.org/support/users/knijia/)
 * (@knijia)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060617)
 * Hi there,
    i wondered how to display ‘related type’ when the field is duplicated.
   When i use :
 *     ```
       <?php>
           $related_content = get_order_field('my-related-field');
           foreach($related_content as $my-related-field){
               echo get('my-related-field',1,$my-related-field);
           }
       ?>
       ```
   
 * it returns the correct IDs of related type, but how to display the_permalink,
   the_title and the_excerpt of each post for example instead of the ID numbers 
   of each post.
    I guess it’s more a php question but if someone could give me 
   an advice. Thks
 *  [paulcham](https://wordpress.org/support/users/paulcham/)
 * (@paulcham)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060622)
 * [@knijia](https://wordpress.org/support/users/knijia/), did you ever get this
   figured out – how to display a duplicated related field?
 * Also, anyone know the way to retrieve Magic Fields data from the related post?
 * Thanks,
    Paul
 *  [paulcham](https://wordpress.org/support/users/paulcham/)
 * (@paulcham)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060623)
 * Oh, of course, I just got it 5 seconds after I posted a question (that happens
   a lot…)
 *     ```
       <?php $stories = get_field_duplicate('related_stories');
           foreach($stories as $story){ ?>
               <?php echo get_permalink($story); ?>
               <?php echo get_the_title($story); ?>
               <?php echo get_the_post_thumbnail($story, 'my-custom-thumbnail'); ?>
           <?php } ?>
       ```
   
 * This is working. I still can’t seem to dig into the post and retrieve Magic Fields
   data. Any ideas out there?
 *  Thread Starter [matstillo](https://wordpress.org/support/users/matstillo/)
 * (@matstillo)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060624)
 * This is a stripped down version of what I have been doing. I hope I haven’t busted
   the code by stripping it down badly!
 *     ```
       <?php // start your duplicated fields loop
       	$related_content_fields = getFieldDuplicates('your_related_content_field_name', 1);
   
       	for($i = 1; $i < $related_content_fields+1; $i++):
   
       	// gets the ID for each related content post/page
       	$related_content_id = get('your_related_content_field_name',1,$i,true);
   
       ?>
   
       	<?php // these work by using the ID we have collected and stored above  ?>
   
       	<?php echo get_the_post_thumbnail($related_content_id, 'your_featured_image'); ?>
       	<a href="<?php echo get_permalink($related_content_id); ?>">
   
       		<h2><?php echo get_the_title($related_content_id); ?></h2>
   
       	</a>
   
       	<?php // this works by just accessing the magic field in the duplicate   ?>
       	<p><?php echo get('magic_fields_name',1,$i); ?></p>
   
       <?php endfor; ?>
       ```
   
 * I’m not sure your code above was calling in the field duplicates quite right,
   as you need to start a count for all the duplicates & use that later. Was that
   definitely working?
 * I did find a bit more code that i have, but I won’t include it here as it might
   get confusing, but it is for when the post or page you are linking to in your
   related type fields ALSO has a related type filed in it & you want to access 
   that info. For example, I used it in the following situation:
 * I had created a post Category that I was using to create calendar events. I used
   the duplicated related type field so that I could create multiple lists of these
   events in a custom loop. In my sidebar for example on any specific page, i could
   add 2 or three events relevant to that page.
 * The code above was used to display both generic WP fields & to create links for
   that event content.
 * What I had done for the event posts, however, was use another related type field
   on each event write panel, which allowed you to link to a ‘venue’ post. Another
   post category for ‘venues’ was created to allow me to build up an archive of 
   all the different venues.
 * If I wanted to be able to access the ‘venues’ magic fields, from within the ‘
   events’ duplicated related type loop, there was additional code required.
 * Like I say, I’ll leave it out unless it sounds useful to you!?
 * I’m pretty sure that what I have done is exceed the recommended use for post 
   categories, but it all works. It should probably be done now using custom taxonomies,
   which are handled much better in the new version of Magic Fields 2, but I haven’t
   got my head around that yet!
 *  Thread Starter [matstillo](https://wordpress.org/support/users/matstillo/)
 * (@matstillo)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060625)
 * Think I did strip things out badly!The bit you are really after, which is to 
   return the magic fields content from the duplicate is not this:
 * `<?php echo get('magic_fields_name',1,$i); ?>`
 * It should be this:
 * `<?php echo get('magic_fields_name', 1, 1, false, get('related_content',1,$i));?
   >`
 * See if that works
 *  [paulcham](https://wordpress.org/support/users/paulcham/)
 * (@paulcham)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060626)
 * [@matstillo](https://wordpress.org/support/users/matstillo/) – thanks for the
   code – that works for me for grabbing Magic Fields data from related type fields!
   Such a useful field type.
 * FYI, the foreach loop that I had above was working but not when I tried to echo
   any MF fields. Which is strange because this is how I echo duplicated groups 
   in MF1 and it works:
 *     ```
       <?php $events= get_group('Event'); // use the Custom Group name
            foreach($events as $event){ ?>
               <h1><?php echo $event['mf_field_name'][1]; ?></h1>
               <p><?php echo $event['mf_field_name'][1]; ?></p>
               <img src="<?php echo $event['mf_field_name'][1]['o']; ?>">
       <?php } ?>
       ```
   
 * I tried MF2 but I had a major problem with the apparent lack of Page Write Panels.
   I am accustomed to using Page Write Panels heavily and found that change to be
   a big problem that I didn’t have time to work around. The Custom Taxonomy tools
   were very nice, however I had to revert to MF1 for this project.
 * Thanks again!
 * -Paul

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Help needed using Magic Fields 'Related Type'’ is closed to new replies.

## Tags

 * [Related Type](https://wordpress.org/support/topic-tag/related-type/)

 * 11 replies
 * 4 participants
 * Last reply from: [paulcham](https://wordpress.org/support/users/paulcham/)
 * Last activity: [14 years, 2 months ago](https://wordpress.org/support/topic/help-needed-using-magic-fields-related-type/#post-2060626)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
