505 posts
  • Bought between 10 and 49 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • United Kingdom
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?

3422 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 100 and 499 items
  • Referred more than 2000 users
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+4 more
sevenspark Volunteer 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/

505 posts
  • Bought between 10 and 49 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • United Kingdom
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

438 posts Don't be a HATER!
  • Exclusive Author
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Bought between 10 and 49 items
  • Referred between 10 and 49 users
  • Won a Competition
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Has been a member for 4-5 years
webinpixels says
echo '<option value="' . $option . '">' . $option . '</option>';

good luck :)

505 posts
  • Bought between 10 and 49 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • United Kingdom
Gareth_Gillman says

Brilliant, thanks WIP … All done

254 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 10 and 49 users
  • Sold between 10 000 and 50 000 dollars
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

505 posts
  • Bought between 10 and 49 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • United Kingdom
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