it-swarm.com.de

Front-End-Beitrag speichern Untergeordnete Begriffe

Ich versuche, mein Front-End-Post-Formular zu aktualisieren, mit dem ich meinen benutzerdefinierten Taxonomie-Elternbegriff speichern kann, aber jetzt muss ich den ausgewählten Kindbegriff speichern.

NHB steht für Neighborhood (Nachbarschaft). Dies sind untergeordnete Begriffe für die Taxonomie des Stadttyps. Hier ist mein Code oben für die relevanten Felder -

 $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;

Und -

   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');

Dann meine Dropdown für das Kind Begriffe auf meinem Post-Formular am vorderen Ende -

<?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>"; 
?>

Wie kann ich das als Kindbegriff speichern und auch ausgeben lassen? Ich gebe derzeit den übergeordneten Begriff wie folgt aus -

<?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

Oben auf meiner Seite habe ich Folgendes, um die Begriffe zu speichern. $ city_type steht für die übergeordneten Begriffe der Taxonomie "city-type". $ nhb steht für die untergeordneten Begriffe, die dem ausgewählten/gespeicherten übergeordneten Begriff entsprechen.

<?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();
    }

  ?>  

Die Form -

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

Die untergeordneten Begriffe Dropdown-Auswahl -

<?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>"; 
?>
4
Rich

Ok, ich habe es mithilfe des @ TomMorton-Vorschlags herausgefunden.

Ich musste meinen Code aktualisieren, um die Bedingungen zu aktualisieren -

$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);

Und das, um sie zu setzen -

$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;
                }

Das Einzige, was ich in meinem Dropdown ändern musste, war, dass ich den Begriff "Name" verwendet habe, ihn aber in "Slug" geändert habe.

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

Vergewissern Sie sich dann, dass die Begriffe festgelegt wurden, indem Sie - verwenden.

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

Wenn Sie untergeordnete Begriffe speichern, können Sie sie meiner Meinung nach der Gruppe $term_array hinzufügen, in der die Eltern wohnen. Aus der Perspektive eines Posts (oder eines benutzerdefinierten Posts) hat das Speichern der Taxonomie nichts mit Eltern oder Kindern zu tun.

Der Kodex WP lautet"Bei hierarchischen Begriffen (z. B. Kategorien) müssen Sie immer die ID und nicht den Termnamen übergeben, um Verwechslungen zu vermeiden, wenn möglicherweise ein anderes Kind mit demselben Namen vorhanden ist . "Dies impliziert, dass Sie in der Lage sein sollten, die untergeordnete ID zu übergeben, und sie wird in den untergeordneten Begriff eingeteilt.

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

Sie haben zwei Möglichkeiten, um die untergeordneten Elemente anzuzeigen. Zum einen können Sie die untergeordneten Elemente basierend auf Ihrem Code weiter durchlaufen:

/*
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 . '';
            }
        }
    }
}

Oder Sie können Kinder durchlaufen, nachdem Sie sie vom Elternteil erhalten haben (für den Fall, dass Sie einen Elternteil haben, aber dessen Kinder benötigen:

/* 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 . '';
    }
}

BEARBEITEN: Zeigt ein Beispiel für eine Speicherfunktion.

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');
2
Tom