4

Im trying to update my front end post form which allows me to save my custom taxonomy parent term, but now I need to save the child term that's selected.

NHB is for Neighborhood which are child terms for the city-type taxonomy. Here's my code on top for the relevant fields --

 $city_type = wp_get_object_terms($Property_Id, 'city-type', array('fields' => 'ids'));
        $city_type = array_reverse($city_type);
        if (!empty($city_type)) {
            $city_term = get_term($city_type[0], 'city-type');
            $city_type_value = $city_term->slug;
        }
$nhb = wp_get_object_terms($Property_Id, 'city-type', array('fields' => 'ids'));
        if (!empty($nhb)) {
            $term = get_term($nhb[0], 'city-type');
            $nhb_type_value = $term->name;

And -

   wp_set_object_terms($pid, $nhb_type_value, 'city-type');

update_post_meta($pid, 'imic_property_custom_city', $property_custom_city);
                $city_for_update = get_term_by('slug', $city_type_value, 'city-type');
                $term_array = array();
                while ($city_for_update->parent != 0) {
                    $city_for_update = get_term_by('id', $city_for_update->parent, 'city-type');
                    array_push($term_array, $city_for_update->slug);
                }
                array_push($term_array, $city_type_value);
wp_set_object_terms($pid, $term_array, 'city-type');

Then my drop down for the child terms on my post form on the front end --

<?php $taxonomyName = "city-type"; 
$parent_terms = get_terms( $taxonomyName, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) );   
echo "<select name='nhb' class='form-control' id='p-nhb'>";
echo "<option class='button' value='Any'>All</option>";
foreach ( $parent_terms as $pterm ) {
    //Get the Child terms
    $terms = get_terms( $taxonomyName, array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false ) );
    foreach ( $terms as $term ) {
$selected = ($nhb_type_value == $term->name) ? "selected" : "";
        echo "<option data-val='" . $pterm->slug . "' value='" . $term->slug . "' ' . $selected . '>" . $term->name . "</option>"; 
    }
}
echo "</select>"; 
?>

How can I get that to save as a child term and also output? I currently output the parent term like this --

<?php  $terms = wp_get_post_terms(get_the_ID(), 'city-type');
  if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
     foreach ( $terms as $term ) {
       echo '' . $term->name . '';      
     }
  } ?>

UPDATE

On top of my page I have the following to save the terms. $city_type is for the "city-type" taxonomy parent terms. $nhb is for the child terms that correspond with the selected/saved parent term.

<?php
/*
  Template Name: Front End Form
 */
get_header();
global $current_user, // Use global
get_currentuserinfo(); // Make sure global is set, if not set it.
$subdraft = $_POST['subdraft'];
$edit_url = imic_get_template_url('template-edit-property-new.php');
if ((user_can($current_user, "administrator"))||(user_can($current_user, "edit_others_posts")) ):
    global $imic_options;
    $msg = '';
    $flag = 0;
    $Property_Id = $property_title = $city_type_value = $nhb_type_value = '';

    if (get_query_var('site')) {
$Property_Id = get_query_var('site');
$property_title = get_the_title($Property_Id);
        $city_type = wp_get_object_terms($Property_Id, 'city-type', array('fields' => 'ids'));
        $city_type = array_reverse($city_type);
        if (!empty($city_type)) {
            $city_term = get_term($city_type[0], 'city-type');
            $city_type_value = $city_term->slug;
        }
$nhb = wp_get_object_terms($Property_Id, 'city-type', array('fields' => 'ids'));
        if (!empty($nhb)) {
            $term = get_term($nhb[0], 'city-type');
            $nhb_type_value = $term->name;
        }


    }
    $Property_Status = get_post_meta(get_the_ID(), 'imic_property_status', true);

// Check if the form was submitted
    if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action'])) {

$property_title = $_POST['title'];

$nhb_type_value = $_POST['nhb'];


if (isset($_POST['textonomies_city']) && !empty($_POST['textonomies_city'])) {
            $reverce_data = array_reverse($_POST['textonomies_city']);
            foreach ($reverce_data as $textonomies_city) {
                if (!empty($textonomies_city)) {
                    $city_type_value = $textonomies_city;
                    break;
                }
            }
            $property_custom_city = '';
        }

        if (($city_type_value == 'other') || ($city_type_value == 'city')) {
            $city_type_value = '';
        }
             if ($msg == '') {
            if (get_query_var('site')) {
                $post = array(
                    'ID' => get_query_var('site'),
                    'post_title' => $property_title,
                    'post_content' => $property_content,
                    'post_date' => $property_listdate_value,
                    'post_status' => 'publish', // Choose: publish, preview, future, etc.
                    'post_type' => 'property'  // Use a custom post type if you want to
                );
                $pid = wp_update_post($post);
                // Pass  the value of $post to WordPress the insert function
                $flag = 1; 
            } else {
                    $post_status = 'draft';
                }
                $post = array(
                    'post_title' => $property_title,
                    'post_content' => $property_content,
                    'post_status' => $post_status,
                    'post_date' => $property_listdate_value,
                    'post_type' => 'property'  // Use a custom post type if you want to
                );
                $pid = wp_insert_post($post);
                $total_property = get_user_meta($current_user->ID, 'property_value', true);
                $new_value = ($total_property != 0) ? ($total_property - 1) : $total_property;
                update_user_meta($current_user->ID, 'property_value', $new_value);
                $flag = 1;
            }

                wp_set_object_terms($pid, $nhb_type_value, 'city-type');


            if ('POST' == $_SERVER['REQUEST_METHOD']) {

// Set Terms For Tax
wp_set_object_terms($pid, $nhb_type_value, 'city-type');

                $city_for_update = get_term_by('slug', $city_type_value, 'city-type');
                $term_array = array();
                while ($city_for_update->parent != 0) {
                    $city_for_update = get_term_by('id', $city_for_update->parent, 'city-type');
                    array_push($term_array, $city_for_update->slug);
                }
                array_push($term_array, $city_type_value);
wp_set_object_terms($pid, $term_array, 'city-type');


if (get_query_var('site')) {
$Property_Id = get_query_var('site');
$property_title = get_the_title($Property_Id);

$nhb = wp_get_object_terms($Property_Id, 'city-type', array('fields' => 'ids'));
                if (!empty($nhb)) {
                    $terms = get_term($nhb[0], 'city-type');
                    $nhb_type_value = $terms->name;
                }


            }
        }
}
if(get_query_var('remove')){
    $delete_id = get_query_var('remove');
    $post_author = get_post_field('post_author',$delete_id);
$user_name= $current_user->ID;
if($post_author==$user_name){
    wp_trash_post($delete_id); }
}
    if (get_query_var('site')) {
        $current_Id = get_query_var('site');
    } else {
        $current_Id = get_the_ID();
    }

  ?>  

The form -

 <form action="" method="post" enctype="multipart/form-data">

The child terms drop down select -

<?php $taxonomyName = "city-type"; 
$parent_terms = get_terms( $taxonomyName, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) );   
echo "<select name='nhb' class='form-control' id='p-nhb'>";
echo "<option class='button' value='Any'>All</option>";
foreach ( $parent_terms as $pterm ) {
    //Get the Child terms
    $terms = get_terms( $taxonomyName, array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false ) );
    foreach ( $terms as $term ) {
$selected = ($nhb_type_value == $term->name) ? "selected" : "";
        echo "<option data-val='" . $pterm->slug . "' value='" . $term->slug . "' " . $selected . ">" . $term->name . "</option>"; 
    }
}
echo "</select>"; 
?>

2 Answers 2

2
+25

With saving child terms, I believe you can add them to the $term_array group where the parent lives. From the perspective of a post (or custom post type) saving the taxonomy has nothing to do with parent or child.

The WP Codex states "For hierarchical terms (such as categories), you must always pass the id rather than the term name to avoid confusion where there may be another child with the same name." This implies that you should be able to pass the child ID and it will be categorized in the child term.

Source: https://codex.wordpress.org/Function_Reference/wp_set_post_terms#Notes

Regarding displaying the children, you can do it one of two ways. The first, you can continue looping through children based on your code:

/*
Display child terms:

Source:
https://developer.wordpress.org/reference/functions/get_term_children/
*/
$parent_terms = wp_get_post_terms(get_the_ID(), 'city-type');
if ( ! empty( $parent_terms ) && ! is_wp_error( $parent_terms ) ){
    foreach ( $parent_terms as $parent ) {
        echo '' . $parent->name . '';

        $children = get_term_children($parent->ID, 'city-type'); //term_id, taxonomy
        if(!empty($children) && ! is_wp_error( $children )){
            foreach($children as $child){
                echo '' . $child->name . '';
            }
        }
    }
}

Or you can loop through children after getting them from the parent (in the case that you may have a parent but need it's children:

/* OR YOU CAN GET CHILDREN DIRECTLY */
$children = get_terms(array(
    'parent' => 10 //ID_OF_PARENT_YOU_WANT
));

if(!empty($children) && ! is_wp_error( $children )){
    foreach($children as $child){
        echo '' . $child->name . '';
    }
}

EDIT: Showing an example save function.

wp_set_object_terms($pid, $nhb_type_value, 'city-type');

update_post_meta($pid, 'imic_property_custom_city', $property_custom_city);
$city_for_update = get_term_by('slug', $city_type_value, 'city-type');
$term_array = array();
while ($city_for_update->parent != 0) {
    $city_for_update = get_term_by('id', $city_for_update->parent, 'city-type');
    array_push($term_array, $city_for_update->slug);
}
array_push($term_array, $city_type_value);

/*Find child ID the user selected*/
while ($child_term->parent != 0) {
    $child_term = get_term_by('id', $child_term->ID, 'city-type');
    //Get child term object and add to term_array
    array_push($term_array, $child_term->slug);
}

wp_set_object_terms($pid, $term_array, 'city-type');
4
  • Code example for saving?
    – 730wavy
    Oct 25, 2017 at 20:51
  • @RichJ I can't give you something specific without understanding how the saving process works. Is there a form submit or some other action? Maybe posting the full code that runs the save functions would help. For now I posted an update that includes an assumption on how you can save the child that was chosen. Note in my example you will have to define $child_term, which is the information I am missing.
    – Tom
    Oct 25, 2017 at 23:20
  • I updated my question
    – 730wavy
    Oct 26, 2017 at 2:53
  • Im still unsure how to apply your answer, please check my update.
    – 730wavy
    Oct 30, 2017 at 20:44
1

Ok I figured it out using @TomMorton suggestion.

I had to updated my code to this which updates the terms --

$city_for_update = get_term_by('slug', $city_type_value, 'city-type');
$term_array = array();
while ($city_for_update->parent != 0) {
    $city_for_update = get_term_by('id', $city_for_update->parent, 'city-type');
    array_push($term_array, $city_for_update->slug);
}
array_push($term_array, $city_type_value);

$child_term = get_term_by('slug', $nhb_type_value, 'city-type');
$term_array = array();
/*Find child ID the user selected*/
while ($child_term->parent != 0) {
    $child_term = get_term_by('id', $child_term->ID, 'city-type');
    //Get child term object and add to term_array
    array_push($term_array, $child_term->slug);
}
array_push($term_array, $nhb_type_value);

And this to set them --

$nhb_type_value = $_POST['nhb'];

$nhb_type = wp_get_object_terms($Property_Id, 'city-type', array('fields' => 'ids'));
$nhb_type = array_reverse($nhb_type);
                if (!empty($nhb_type)) {
                    $terms = get_term($nhb_type[0], 'city-type');
                    $nhb_type_value = $terms->slug;
                }

The only thing I had to change on my drop down was this, I was using term name but I changed it to term slug -

$selected = ($nhb_type_value == $term->slug) ? "selected" : "";

Then just make sure the terms are being set, using --

wp_set_object_terms($pid, $nhb_type_value, 'city-type');

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.