• Hi WordPress,
    I’ve got a custom post type setup with various custom attributes as inputs that work fine. I am trying to setup a custom value as a dropdown but I can’t seem to get the selected value. It’s for an age dropdown for ‘years’ and ‘months’

    functions.php:

    <p><label>Age:</label><br />
      <select name="months">
    <?php
        foreach (range(0,11) as $months)
        {
            ?>
    <option name="months" value="<?php echo $months;?>"><?php echo $months . ' Months';?></option>
            <?php
        }
    ?>
    </select>
    <select name="years">
    <?php
        foreach (range(0,18) as $years)
        {
    ?>
    <option name="years" value="<?php echo $years;?>"><?php echo $years . ' Years';?></option>
            <?php
        }
    ?>
    </select></p>
    function stats(){
      global $post;
      $months = $custom["months"][0];
      $years = $custom["years"][0];
    
    function save_details(){
      global $post;
      update_post_meta($post->ID, "years", $_POST["years"]);
      update_post_meta($post->ID, "months", $_POST["months"]);
    }

    This works with other fields like inputs but it does not work for the selection from my dropdown. You can select a value but it is not saved. Any help is appreciated. Thank you.

Viewing 1 replies (of 1 total)
  • So a couple things stick out.

    1) You’re using range 0 for months which means that January is going to be value 0 and December will be value 11 ( instead of 1 and 12 as you would expect months numbers to be ). If you’re not accounting for the -1 offset this may return unexpected results.

    2) By default forms will submit as $_GET, I don’t see a form here so make sure that your method attribute is set to POST if you need to get the value via $_POST.

    3) What calls save_details? Are you sure that function is running as expected?

    You can always turn on debugging, specifically WP_DEBUG_LOG, and print out all the POSTed values to see what you have to work with at that point:

    error_log( print_r( $_POST, 1 ) );

Viewing 1 replies (of 1 total)
  • The topic ‘Get Value from Custom Select’ is closed to new replies.