0

I have two post types in my website. one for courses and other for teachers. I want to put a selectable list of teachers post type in courses post type and then show it in course single page and link it to single page of that teacher. how can i do?

 function custom_meta_box_markup($object)
  {
 wp_nonce_field(basename(__FILE__), "meta-box-nonce");
 ?>
  <div>
  <select name="meta-box-dropdown">
  <?php 
  $args=array('post_type'=>'mama_ashpaz');
  $q=new WP_Query($args);


   if($q->have_posts()){
   $current=0;
     while($q->have_posts()){
       $q->the_post();?>
  <?php $options[$current]=get_the_title();
    $options_id[$current]=get_the_id();
    $current++;

    }

    }wp_reset_postdata();
    foreach($options as $key=>$value){
    if($value == get_post_meta($object->ID, "meta-box-dropdown", true))
     {
     ?>
     <option selected><?php echo $value; ?></option>
    <?php    
     }
    else
    {
     ?>
   <option><?php echo $value; ?></option>
    <?php
    }

   }
    ?>
   </select>
   </div>
   <?php  
  }

   function add_custom_meta_box()
   {
    add_meta_box("demo-meta-box", "مامان آشپز", "custom_meta_box_markup", 
  "product", "side", "high", null);
   }

   add_action("add_meta_boxes", "add_custom_meta_box");



    function save_custom_meta_box($post_id, $post, $update)
    {
    if (!isset($_POST["meta-box-nonce"]) || !wp_verify_nonce($_POST["meta- 
    box-nonce"], basename(__FILE__)))
     return $post_id;

      if(!current_user_can("edit_post", $post_id))
      return $post_id;

      if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
       return $post_id;

     $slug = "product";
     if($slug != $post->post_type)
     return $post_id;

    $meta_box_dropdown_value = "";

    if(isset($_POST["meta-box-dropdown"]))
     {
     $meta_box_dropdown_value = $_POST["meta-box-dropdown"];
     }   
    update_post_meta($post_id, "meta-box-dropdown", 
    $meta_box_dropdown_value);

    }

    add_action("save_post", "save_custom_meta_box", 10, 3);

       function add_maman(){?>
     <div class="maman-meta"><a href="<?php echo get_the_permalink();?>">
    <?php echo get_post_meta( get_the_id(), "meta-box-dropdown", true ) ;?>
    </a>
    </div>
    <?php
     }
    add_action( 'woocommerce_before_single_product_summary','add_maman' );
  • To be clear, you are trying to allow the user to select a teacher from the courses EDIT screen? So you can associate a teacher with a course? – Tom Jun 9 '18 at 14:09
  • no.. i want to select a teacher for each course – Farhang Jun 9 '18 at 15:20
0

I cannot create a complete solution here, because it's kinda a 'work for me for free' question.

I can (globally) tell you how to accomplish this.

  1. wp-admin --> courses CPT --> add a metabox 'select teacher(s)'. Collect the teachers with get_posts(). Save the selected teachers as meta, use update_post_meta($course_post_id, 'assigned_teachers', $assigned_teachers) in metabox save function.
  2. In courses single page template, collect the assigned teachers with get_post_meta($course_post_id, 'assigned_teachers', true). If you want to get the link to a teacher post use get_permalink($teacher_post_id).

I hope this is enough to get you started.


UPDATE ( OP added code )

Your first function:

function custom_meta_box_markup($object)
 {
wp_nonce_field(basename(__FILE__), "meta-box-nonce");
?>
 <div>
 <select name="meta-box-dropdown">
 <?php
 $args=array('post_type'=>'mama_ashpaz');
 $q=new WP_Query($args);


  if($q->have_posts()){
  $current=0;
    while($q->have_posts()){
      $q->the_post();?>
 <?php $options[$current]=get_the_title();
   $option[$current]=get_the_id();
   $current++;

   }

   }wp_reset_postdata();
   foreach($options as $key=>$value){
   if($value == get_post_meta($object->ID, "meta-box-dropdown", true))
    {
    ?>
    <option selected><?php echo $value; ?></option>
   <?php
    }
   else
   {
    ?>
  <option><?php echo $value; ?></option>
   <?php
   }

  }
   ?>
  </select>
  </div>
  <?php
 }

Is kinda messy, and you're not creating the <option> correctly.

Make it like this:

function custom_meta_box_markup($product)

  $options_html = '';

  $args = array(
    'posts_per_page' => -1, // get all posts
    'post_type' => 'mama_ashpaz',
  );
  $teacher_posts = get_posts($args);

  $selected_teacher = get_post_meta($product->ID, "selected_teacher", true); // notice the different naming, this is more clear.

  if($teacher_posts) {
    foreach($teacher_posts as $teacher) {

      $selected = '';
      if($teacher->ID == $selected_teacher) $selected = 'selected="selected"';

      // in your code you are not setting the option value.
      // When you save the form and collect it with save_post action hook. Only the 'option value=""' will be passed to the $_POST.
      $options_html .= '<option value="'.$teacher->ID.'" '.$selected.'>'.$teacher->post_title.'</option>';
    }
  }

  // start the Output
  ?>
  <select name="product_teachers_select">
  <?php echo $options_html; ?>
  </select>
  <?php
}

// If you're keeping the above naming, make sure you update the remaining follow-up functions

Now the teacher id is saved. When you retrieve the teacher you will have his id. With his id you can collect any data you want, example:

$teacher = get_post($teacher_id);
$teacher_description = $teacher->post_content;
$teacher_custom_meta = get_post_meta($teacher->ID, 'the_custom_meta_key', true);

Tip:
when you create your code, try to name your params, meta_keys, functions etc. in a way that anyone can understand what's going on. Even for yourself this is very important (imagine looking at your code 2 years from now).

Regards, Bjorn

  • I don't see any code, but nevermind... I think you can better try the answer from @dhirenpatel22. With ACF it's a bit easier to understand everything. And you probably find better guides with Google. Please accept his answer if you go that way. Thanks! – Bjorn Jun 9 '18 at 11:28
  • Hi.. I put my code.. I created dropdown metabaox and print selected option in single page of product.how I can retrive selected option's ID and put it's permalink in single page? thanks for your favour. – Farhang Jun 10 '18 at 5:44
  • Please understand. I can't see your code from here. It's difficult to tell what to do. When you save the selected teachers, save the id's. When you retrieve the selected teachers you will have their id's, with that you can get any data you want. – Bjorn Jun 10 '18 at 6:07
  • I edited my quesion and put the code. don't see it? – Farhang Jun 10 '18 at 6:19
  • yeah i see it now. one sec, i'll edit my answer. – Bjorn Jun 10 '18 at 6:23
0

You will require a custom meta box with a drop-down field.

Here's a link. It properly teaches how to use and save the data from the meta box.

On that meta box, you can add WP_Query to get the list of your teacher post type. You can set the option value as ID which you can use to query on the front end.

On the frontend, you can use the get_post_meta function to get the saved value from your dropdown.

Then, use get_permalink function and pass in the meta value as an argument to get the full permalink of the teacher.

That's it.

  • thanks. I created a dropdown metabox from post type in product post type. but how can save selected meta and then print it in single page of product with it's permalink? – Farhang Jun 9 '18 at 14:03
0

You can use third party free WordPress plugin "ACF" to add the additional custom field.

You can add relationship field and select post type which you want to display in another post type as shown in below screenshot.

enter image description here

Use below code in the single page to display selected posts.

<?php $posts = get_field('relationship_field_name');
if( $posts ): ?>
  <ul>
  <?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
    <li>
        <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
        <span>Custom field from $post: <?php the_field('author', $p->ID); ?></span>
    </li>
  <?php endforeach; ?>
  </ul>
<?php endif; ?>

Find the brief explanation of how to use relationship field in below reference URL: https://www.advancedcustomfields.com/resources/relationship/

  • thanks.. what about Post Object in ACF?.. and i want to print custom meta in single page with it's permalink. – Farhang Jun 9 '18 at 14:00
  • You can select "Post Objects" as return format when creating relationship custom field in ACF. For permalinks of selected posts, you can use get_permalink, which I have already shown in above code. – dhirenpatel22 Jun 9 '18 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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