1073 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+5 more
Gareth_Gillman
says

Hi guys!

I am making a WP theme and it’s made up of 2 custom post types, I would like to add a custom meta box to the one post type with a list of the other post type,

I then need to be able to select items from this list, and they will show up on the page for the custom post type.

It’s slightly confusing so will make it easier below:

Custom Post Type 1 has a selectable list of elements within Custom Post Type 2, on each individual page for CPT1 , it would show the information from the selected elements from CPT2 .

Does anyone know of any tutorials to do what I want, or can point me in the right direction?

3535 posts
  • Power Elite Author: Sold more than $1M on Envato Market
  • Has sold $1M+ on Envato Market and is now a Power Elite Author
  • Had an item that was trending
  • Located in United States
+13 more
sevenspark
Moderator
says

All you’d need is to use get_posts() and search by post type, then parse the results into a select box.

Alternatively, this plugin might suit your needs: http://wordpress.org/extend/plugins/posts-to-posts/

1073 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+5 more
Gareth_Gillman
says

Thanks SS, nearly there but got a PHP error..which I can never work out… PHP isn’t my strongest point as I get along with copying code to do what I want.

The code with the error is:
function activity_meta() {
  global $post;
  echo '<input name="activitymeta_noncename" id="activitymeta" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" type="hidden" />';
  $option_list = wp_list_pages( 'post_type=Partners&title_li=' );
  echo '<select name="activitymeta_sel" id="activitymeta_sel">';
  foreach ($option_list as $option) {
   echo '<option value="'$option'">', $option, '</option>';
  }
  echo '</select>';
 }
The error is:
Parse error: syntax error, unexpected T_VARIABLE, expecting ’,’ or ’;’ in …..

Which falls on the } after the foreach, I am guessing I am doing something wrong with the foreach and the echo’s but have no idea what I am doing.

Any help at all is great,

Cheers

461 posts Don't be a HATER!
  • Had an item that became a weekly top seller
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
+6 more
CodeElite
says
echo '<option value="' . $option . '">' . $option . '</option>';

good luck :)

1073 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+5 more
Gareth_Gillman
says

Brilliant, thanks WIP … All done

254 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
+4 more
PabloFierro
says

You should read this article as well, http://austinpassy.com/2010/03/creating-custom-metaboxes-and-the-built-in-uploader/

I found it very useful and included that in one of my projects.

Cheers

1073 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+5 more
Gareth_Gillman
says

Sorted out the drop down using a get_pages query but now the meta box won’t save the function…

My code is

function activity_meta() {
  global $post;
  echo '<input name="activitymeta_noncename" id="activitymeta" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" type="hidden" />';
 $pages = get_posts(array('post_type' => 'partners', 'posts_per_page'=> -1, 'post_status' => 'publish'));
  echo '<select name="_sel" id="activitymeta_sels">';
  echo '<option value="">Select Partners</option> ';

foreach ( $pages as $page ) {
   $option = '<option value="' . $page->post_title . '">';
   $option .= $page->post_title;
   $option .= '</option>';
   echo $option;
  }
 echo '</select>';
 }

 function wpt_save_events_meta($post_id, $post) {
  if ( !wp_verify_nonce( $_POST['activitymeta_noncename'], plugin_basename(__FILE__) )) {
   return $post->ID;
  }
  if ( !current_user_can( 'edit_post', $post->ID ))
  return $post->ID;

  $events_meta['_sel'] = $_POST['_sel'];

  foreach ($events_meta as $key => $value) {
   if( $post->post_type == 'revision' ) return;
   $value = implode(',', (array)$value); 
   if(get_post_meta($post->ID, $key, FALSE)) {
    update_post_meta($post->ID, $key, $value);
   } else {
    add_post_meta($post->ID, $key, $value);
   }
   if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
  }
 }
 add_action('save_post', 'wpt_save_events_meta', 1, 2);

Any help is gratefully received.

by
by
by
by
by
by