1

I'm hoping someone can steer me in the right direction. I have a custom post type called 'locations'. I have another custom post type called 'testimonials'.

What I'd really like to do is be able to associate a 'testimonial' with a 'location'. So I was trying to figure out if I could some how list those locations within the testimonial post page in the admin area. Almost like a meta box.

I haven't been able to find anything like this anywhere.

Any and all help is appreciated!

6
  • I think you may use "testimonial" custom taxonomy instead of custom post type. Would that help you to achieve goal?
    – Saikat
    May 7, 2014 at 3:19
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. May 7, 2014 at 3:37
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. May 7, 2014 at 3:37
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
    – tfrommen
    May 7, 2014 at 5:51
  • A custom taxonomy maybe able to be used, however I already have a custom post type with all my locations. Locations have specific details and will need to be added to in the long run.
    – lz430
    May 7, 2014 at 17:37

2 Answers 2

1

Yes, you can.

You would need to add a custom field that would store the ID of other post type. I would recommend using a select list.

You could try something along these lines:

add_action( 'add_meta_boxes', 'wpse_143600_add_box' );
add_action( 'save_post', '143600_save_box' );

//create the metabox
function wpse_143600_add_box() {
   add_meta_box( 
    'related_testimonial',
    __( 'Testimonails', 'wpse_143600_translation' ),
    'wpse_143600_testimonial_box',
    'locations',
    'normal'
    );
}

//build the box
function wpse_143600_testimonial_box($post) {

wp_nonce_field( basename( __FILE__ ), 'wpse_143600_nonce' );

$wpse_143600_stored_meta = get_post_meta( $post->ID );

$testimonialArgs = array(
'post_type' => 'testimonials',
'post_status' => 'publish',
'numberposts' => -1
);

$testimonials = get_posts($testimonialArgs);

if($testimonials): ?>
<p>
  <label for="meta-select" class="wpse_143600-row-title"><?php _e( 'Example Select Input', 'wpse_143600_translation' )?></label>
  <select name="meta-select" id="meta-select">
    <option value="NULL">Please choose a testimonial…</option>
<?php foreach($testimonials as $testimonial): ?>
    <option value="<?php echo $testimonial->ID; ?>" <?php if ( isset ( $wpse_143600_stored_meta['meta-select'] ) ) selected( $wpse_143600_stored_meta['meta-select'][0], $testimonial->ID ); ?>><?php echo $testimonial->post_title; ?></option>
<?php endforeach; ?>
  </select>
</p>    
<?php
else:
?>
<p>There are no testimonials - please save this post, and write some testimonials. You'll then be able to choose a testimonial for this location.</p>
<?php
endif;
}

//save the box
function wpse_143600_save_box( $post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'wpse_143600_nonce' ] ) && wp_verify_nonce( $_POST[ 'wpse_143600_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
}
// Checks for input and saves if needed
if( isset( $_POST[ 'meta-select' ] ) ) {
update_post_meta( $post_id, 'meta-select', $_POST[ 'meta-select' ] );
}
}

That's fairly rough code, but should get you close.

Sorry about the length, you can find it on pastebin: http://pastebin.com/zA4aDiV9

3
  • So close! I'm not sure how to get it to save. I had to revise the code so it showed on the testimonial post type rather than the location post type. See the pastebin here: pastebin.com/VjWubfL9
    – lz430
    May 13, 2014 at 1:41
  • Any chance you can show me how to save it properly?
    – lz430
    May 14, 2014 at 2:15
  • Yes - sorry about the delay. I mistyped the save function name in the very second line of the code: add_action( 'save_post', '143600_save_box' ); should be add_action( 'save_post', 'wpse_143600_save_box' ); In any event, you should change all the wpse_143600 to something a bit more specific to your theme. Updated code can be found here pastebin.com/mr6ym8vs Hope that gets you there. There are a few other labeling and text changes you'll need to update. May 14, 2014 at 22:34
1

You can use the post 2 post plugin by scribu. It does exactly what you want and is the most advanced plugin for that purpose.

2
  • Unfortunately I'm not allowed to use plugins. I was hoping there'd be a function I could add to functions.php.
    – lz430
    May 7, 2014 at 17:36
  • This plugin is still working in 2023, very impressive! It's also on GitHub: github.com/scribu/wp-posts-to-posts Jan 12, 2023 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.