Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

2olive on "Get post meta to show in admin"

$
0
0

Hi all.
I'm writing a plugin for a swimming team. I have two custom post types (swimmers and competitions) with their respective meta box.
The titles of those CPTs are the athletes' name and the competitions' name.
I've created a custom table to insert the time for each athlete in each competition. I followed this tutorial
http://mac-blog.org.ua/wordpress-custom-database-table-example-full/

In a previous version of the plugin I had to input all data (name of competition, name of athlete and time) in this last table.
Now I'm trying to get the data already saved and show them in the admin page.
I'm able to show the name of competitions with this code

<td>
 <?php
   $selected = 0;
   $post_type = 'competitions';
   $post_type_object = get_post_type_object($post_type);

   $posts = get_posts(array('post_type'=> $post_type,'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1, 'order' => 'ASC', 'orderby' => 'title' ));

  echo '<select name="name" id="name" style="width: 95%">';

  foreach ($posts as $post) {
    setup_postdata( $post );
    echo '<option value="', $post->ID, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>';

   $selected = $post->ID;
}
  echo '</select>';
?>
</td>

if ($selected)
   $item['comp_date'] = get_post_meta( $post->ID, '_comp_data', true );
<td>
  <input id="comp_date" name="comp_date" type="date" style="width: 95%" value="<?php echo esc_attr($item['comp_date']); ?>"          size="50" class="code" ?>">
</td>
...
<?php wp_reset_postdata(); ?>

I can retrieve the post meta (in this case the date of the competition) but if I select another one, this data remains the same. How can I fix this issue? How can I update the data? I try to explain myself with an example:

In the admin page for the custom table
I select competition 1 so
date field is populated with date for competition 1
pool field is populated with pool of competition 1

If I select competition 2
date field is populated with date for competition 2
pool field is populated with pool of competition 2

but all I get is
Select competiton 2
date field is populated with date for competition 1
pool field is populated with pool of competition 1

Anyone can help?
Thanks in advance


Viewing all articles
Browse latest Browse all 8245

Trending Articles