896 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+4 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?

3503 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 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/

896 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+4 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

451 posts Don't be a HATER!
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+6 more
webinpixels says
echo '<option value="' . $option . '">' . $option . '</option>';

good luck :)

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

Brilliant, thanks WIP … All done

254 posts
  • Has been part of the Envato Community for over 3 years
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 1+ items on Envato Market
+3 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

896 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+4 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.

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by