1

I have a dynamic dependent select dropdown system that selects vehicle makes and models from a database which works fine but I would like to convert it to use and select parent and child categories from wordpress rather than using the information from the sql dump. I have the post meta make and the post meta model and the model meta are child categories of the make meta. I'm just wondering how do I write my function to select the child categories of the selected parent categories and display them by AJAX. Any help would be greatly appreciated!

Here is the php function I have so far:

function make_model() {
$vehicle_make = $_REQUEST['id'];
global $wpdb;
$models = $wpdb->get_results($wpdb->prepare("SELECT * FROM car_models WHERE model_id='".$vehicle_make."'",13,'gargle'),ARRAY_A);
foreach($models as $model) { 
echo '<option value="'.$model["model_id"].'">'.$model["model"].'</option>';
}
//echo $models;
die(); 
}
add_action('wp_ajax_make_model' , 'make_model');
add_action('wp_ajax_nopriv_make_model','make_model');

Here is the html that outputs the select dropdown:

<div id="contents">

<form name="form" method="post" action="">

    <label>Make: </label>

    <select name="vehicle_make" class="vehicle_make">

    <option>Select Make</option>

    <?php
    global $wpdb;
    $vehicles = $wpdb->get_results($wpdb->prepare("SELECT * FROM car_makes",13,'gargle'),ARRAY_A);
    foreach($vehicles as $vehicle) { 
    ?>

    <option value="<?php echo $vehicle["make_id"]; ?>"><?php echo $vehicle["make"]; ?></option>

    <?php } ?>

    </select>

    <label>Model: </label>

    <select name="vehicle_model" class="vehicle_model">

    <option>Select Model</option>

    </select>

</form>

and finally the ajax jquery:

$('.vehicle_make').change(function(){   
    var vehicle_make = $(this).val();
    $.ajax({
        cache: false,
        type: "POST",
        url: ajaxurl,
        data: {
            action : 'make_model',
            id : vehicle_make,
        },
        success: function(vehicle_model_data) {
            console.log(vehicle_model_data);
            $('.vehicle_model').html(vehicle_model_data);
        } 
    });
});
  • Which parts of the code that you need help with? – Sally CJ Jul 25 '18 at 11:39
  • @SallyCJ I need help with the php that connects to the database, instead of it connecting to the tables that I created and uploaded an sql dump into, I want to connect it to the wordpress post meta table where car_makes would be the parent category and car_models would be the child category of that parent category. Thank you in advance. – Matt H Jul 25 '18 at 20:17
  • Is the drop-down being displayed on single posts pages? Or are you able to send the post ID via the form? Because it's needed for querying the post meta table. Also, the car_makes and car_models, their values are category IDs, right? Is the value a comma-separated list or an array (i.e. serialized string)? – Sally CJ Jul 26 '18 at 0:45
  • @SallyCJ Yes it's being displayed on a single post and a post archive page. Basically the way it works is the car_makes database has 2 tables, make which is the name of the make of the car and make_id which is an id number of the car make. car_models contains 3 tables models which is the name of the model of car, model_id and make_id which is meant to match the make_id from the car_make database. The ajax fetches the make_id from the data attribute of the dropdown of the car make and matches it to the model in the car_models database. – Matt H Jul 26 '18 at 22:54
  • Ok, but I was actually referring to "connect it to the wordpress post meta table where car_makes would be the parent category and car_models would be the child category of that parent category" - are these post meta already exist and filled with the proper value(s)? If so, what are the values? Category IDs? – Sally CJ Jul 26 '18 at 23:49

Your Answer

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

Browse other questions tagged or ask your own question.