3

So, here's the deal:

I have two category dropdowns, the first for the parent categories and the second for the child categories. They form a chain so that when you select the parent category the second dropdown will be populated with its child categories. Here's the code in the form itself:

    <script type="text/javascript">
    $(function()
    {
    $('#main_cat').change(function()
    {
        var $mainCat=$('#main_cat').val();

        // call ajax
        $("#sub_cat").empty();
        $.ajax
        (
            {
                url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",     
                type:'POST',
                data:'action=my_special_ajax_call&main_catid=' + $mainCat,

                success:function(results)
                {
                    //  alert(results);
                    $("#sub_cat").removeAttr("disabled");       
                    $("#sub_cat").append(results);  
                }
            }
        );                                    
    });
    });
    </script>
    <fieldset class="site-url" class="fieldset-first">
    <?php wp_dropdown_categories('show_count=0&selected=-1&hierarchical=1&depth=1&hide_empty=0&exclude=1&show_option_none=Main Categories&name=main_cat&taxonomy=category');
    ?>
</fieldset>
<fieldset class="site-url" class="fieldset-first">
<select name="sub_cat" id="sub_cat" disabled="disabled"></select>
</fieldset>

Here's the code in functions.php:

function implement_ajax() {
if(isset($_POST['main_catid']))
    {
    $categories=  get_categories('child_of='.$_POST['main_catid'].'&hide_empty=0'.'&taxonomy=category'.'&show_count=0');
      foreach ($categories as $cat) {
        $option .= '<option value="'.$cat->term_id.'">';
        $option .= $cat->cat_name;
        $option .= ' ('.$cat->category_count.')';
        $option .= '</option>';
      }
      echo '<option value="-1" selected="selected">Sub Categories</option>'.$option;
    die();
    } // end if
}
add_action('wp_ajax_my_special_ajax_call', 'implement_ajax');
add_action('wp_ajax_nopriv_my_special_ajax_call', 'implement_ajax');

The problem is that when saving the values like this:

'post_category' =>   array($_POST['main_cat']),

I'll only get the parent category saved for the post. How on earth can I have the parent AND the child categories both saved? Bear in mind that only one category should be selected from each dropdown, no multiples.

Cheers!

UPDATED WITH WHOLE POST POSTING/SAVING CODE BELOW:

<?php       
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

    // Do some minor form validation to make sure there is content

    require_once('recaptchalib.php');
        $privatekey="Oh dear I shouldn't have put my super private API key here for all to see!";

        $resp = recaptcha_check_answer ($privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["my_recapthca_challange_field"],
                                        $_POST["recaptcha_response_field"]);

if ($resp->is_valid) {

    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
        $_SESSION['form_values']['title'] = $_POST['title'];
    } 

    if (isset ($_POST['site-link'])) {
        $siteurl =  $_POST['site-link'];
        $_SESSION['form_values']['site-link'] = $_POST['site-link'];
    }

if (isset ($_POST['first-url'])) {
        $firsturl =  $_POST['first-url'];
        $_SESSION['form_values']['first-url'] = $_POST['first-url'];
    }

if (isset ($_POST['price'])) {
        $price =  $_POST['price'];
        $_SESSION['form_values']['price'] = $_POST['price'];
    }

    if (isset ($_POST['name'])) {
        $devname =  $_POST['name'];
        $_SESSION['form_values']['name'] = $_POST['name'];   
    } 

    if (isset ($_POST['second-url'])) {
        $secondurl =  $_POST['second-url'];
         $_SESSION['form_values']['second-url'] = $_POST['second-url'];
    } 

    if (isset ($_POST['country'])) {
        $devcountry =  $_POST['country'];
         $_SESSION['form_values']['country'] = $_POST['country'];
    }

    if ($_POST['twitter']=="Twitter Username" || $_POST['twitter']=="Not Available")  {
        $twitter =  "Not Available";
         $_SESSION['form_values']['twitter'] = $twitter;
    }else  {
        $twitter = $_POST['twitter'];
        $_SESSION['form_values']['twitter'] = $_POST['twitter'];
    }

    $design =  $_POST['design'];
       if ($design=="yes"){$design = "Yes, this is new!";}
        else {$design="No, this is old!";}


        $related =  $_POST['related'];
       if ($related=="yes"){$related = "Yes, this is related!";}
        else {$related="No, this is not related!";}

$responds =  $_POST['responds'];
       if ($responds=="yes"){$responds = "Yes";}
        else {$responds="No";}

$locale =  $_POST['locale'];
       if ($locale=="yes"){$locale = "Yes";}
        else {$locale="No";}

$widgets =  $_POST['widgets'];
       if ($widgets=="yes"){$widgets = "Yes";}
        else {$widgets="No";}

$skins =  $_POST['skins'];
       if ($skins=="yes"){$skins = "Yes";}
        else {$skins="No";}

$contact =  $_POST['contact'];
       if ($contact=="yes"){$contact = "Yes";}
        else {$contact="No";}

$press =  $_POST['press'];
       if ($press=="yes"){$press = "Yes";}
        else {$press="No";}

$comm =  $_POST['comm'];
       if ($comm=="yes"){$comm = "Yes";}
        else {$comm="No";}

$page =  $_POST['page'];
       if ($page=="yes"){$page = "Yes";}
        else {$page="No";}

$psd =  $_POST['psd'];
       if ($psd=="yes"){$psd = "Yes";}
        else {$psd="No";}

$format =  $_POST['format'];
       if ($format=="yes"){$format = "Yes";}
        else {$format="No";}

$codes =  $_POST['codes'];
       if ($codes=="yes"){$codes = "Yes";}
        else {$codes="No";}


     if ($_FILES['image']['error']==0) { 

    // ADD THE FORM INPUT TO $new_post ARRAY
    $new_post = array(
    'post_title'    =>   $title,
'post_category' =>   array($_POST['main_cat']),
    'post_status'   =>   'draft',
    'post_type' =>   'post'
    );

    $pid = wp_insert_post($new_post);
    add_post_meta($pid, 'URL', $siteurl, true);
    add_post_meta($pid, 'Name', $name, true);
    add_post_meta($pid, 'First URL', $firsturl, true);
    add_post_meta($pid, 'Country', $country, true);
    add_post_meta($pid, 'Twitter', $twitter, true);
    add_post_meta($pid, 'Redesign', $redesign, true);
    add_post_meta($pid, 'Related', $related, true);
add_post_meta($pid, 'Responds', $responds, true);
add_post_meta($pid, 'Locale', $locale, true);
add_post_meta($pid, 'Widgets', $widgets, true);
add_post_meta($pid, 'Skins', $skins, true);
add_post_meta($pid, 'Contact', $contact, true);
add_post_meta($pid, 'Press', $press, true);
add_post_meta($pid, 'Comm', $comm, true);
add_post_meta($pid, 'Page', $page, true);
add_post_meta($pid, 'PSD', $psd, true);
add_post_meta($pid, 'Format', $format, true);
add_post_meta($pid, 'Second URL', $secondurl, true);
add_post_meta($pid, 'Price', $price, true);
add_post_meta($pid, 'Codes', $codes, true);

wp_set_post_categories($post_id, $_POST['main_cat']);
    wp_set_post_tags($post_id, $_POST['post_tags']);


    foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$pid);

    }
    $imagedata = simplexml_load_string(get_the_post_thumbnail($pid));
    $post_img = $imagedata->attributes()->src;

    include_once("script/colors.inc.php");
    $image = $post_img;
    $ex=new GetMostCommonColors();
    $ex->image = $image;
    $colors=$ex->Get_Color();
    $how_many=10;
    $colors_key=array_keys($colors);
    $colortag = $colors_key[1];
    for ($i = 2; $i <= $how_many; $i++)
    {
    $colortag = $colortag.",".$colors_key[$i];
    }

    wp_set_post_tags($pid, $colortag);
     $_SESSION['error_message'] = 'Submitted successfully.';
     $_SESSION['form_values'] = array();
     do_action('wp_insert_post', 'wp_insert_post');

    }

} else {
    if (isset ($_POST['title'])) {
        $name =  $_POST['title'];
        $_SESSION['form_values']['title'] = $_POST['title'];
    } 

    if (isset ($_POST['site-link'])) {
        $siteurl =  $_POST['site-link'];
        $_SESSION['form_values']['site-link'] = $_POST['site-link'];
    }

if (isset ($_POST['first-url'])) {
        $firsturl =  $_POST['first-url'];
        $_SESSION['form_values']['first-url'] = $_POST['first-url'];
    }

if (isset ($_POST['price'])) {
        $price =  $_POST['price'];
        $_SESSION['form_values']['price'] = $_POST['price'];
    }

    if (isset ($_POST['name'])) {
        $name =  $_POST['name'];
        $_SESSION['form_values']['name'] = $_POST['name'];

    }

    if (isset ($_POST['second-url'])) {
        $secondurl =  $_POST['dev-url'];
         $_SESSION['form_values']['second-url'] = $_POST['second-url'];
    }

    if (isset ($_POST['country'])) {
        $country =  $_POST['country'];
         $_SESSION['form_values']['country'] = $_POST['country'];
    }

    if ($_POST['twitter']=="Twitter Username" || $_POST['twitter']=="Not Available")  {
        $twitter =  "Not Available";
         $_SESSION['form_values']['twitter'] = $twitter;
    }else  {
        $twitter = $_POST['twitter'];
        $_SESSION['form_values']['twitter'] = $_POST['twitter'];
    }

    $redesign =  $_POST['redesign'];
       if ($redesign=="yes"){$redesign = "Yes, this is new!";}
        else {$redesign="No, this is old!";}


        $related =  $_POST['related'];
       if ($related=="yes"){$related = "Yes, this is related!";}
        else {$related="No, this is not related!";}

$responds =  $_POST['responds'];
       if ($responds=="yes"){$responds = "Yes";}
        else {$responds="No";}

$locale =  $_POST['locale'];
       if ($locale=="yes"){$locale = "Yes";}
        else {$locale="No";}

$widgets =  $_POST['widgets'];
       if ($widgets=="yes"){$widgets = "Yes";}
        else {$widgets="No";}

$skins =  $_POST['skins'];
       if ($skins=="yes"){$skins = "Yes";}
        else {$skins="No";}

$contact =  $_POST['contact'];
       if ($contact=="yes"){$contact = "Yes";}
        else {$contact="No";}

$press =  $_POST['press'];
       if ($press=="yes"){$press = "Yes";}
        else {$press="No";}

$comm =  $_POST['comm'];
       if ($comm=="yes"){$comm = "Yes";}
        else {$comm="No";}

$page =  $_POST['page'];
       if ($page=="yes"){$page = "Yes";}
        else {$page="No";}

$psd =  $_POST['psd'];
       if ($psd=="yes"){$psd = "Yes";}
        else {$psd="No";}

$format =  $_POST['format'];
       if ($format=="yes"){$format = "Yes";}
        else {$format="No";}

$codes =  $_POST['codes'];
       if ($codes=="yes"){$codes = "Yes";}
        else {$codes="No";}
     $_SESSION['error_message'] = 'You will need to enter the correct captcha value. Please try again!';
        }

}

?>
10
  • Can we see your save code in full rather than just one line?
    – Tom J Nowell
    Jul 5, 2012 at 13:21
  • 1
    Sure, I've updated the question.
    – Andrew
    Jul 5, 2012 at 14:26
  • hmmm you have a lot of repetitive code there you could simplify into an array and a for loop
    – Tom J Nowell
    Jul 5, 2012 at 15:39
  • e.g.: $fields = array('price,','first-url','site-link'); foreach($fields as $field){ if (isset ($_POST[$field])) { $$field = $_POST[$field]; $_SESSION['form_values'][$field] = $_POST[$field]; }
    – Tom J Nowell
    Jul 5, 2012 at 15:41
  • Yeah, by no means it's polished. Thanks for the suggestions, will use them down the line. What about the category problem though? I've understood you can only have one "value" in $_POST['main_cat'], so no 'main_cat','sub_cat', etc. I get error from them anyway.
    – Andrew
    Jul 5, 2012 at 16:00

1 Answer 1

1

To fix this you will need to pass in multiple terms rather than a single term

so instead of:

$_POST['main_cat']

Use:

array($_POST['main_cat'],$_POST['sub_cat'])

You shouldn't need to save main_cat though. Assigning sub_cat should imply it's also in subcat's parent term

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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