web-dev-qa-db-fra.com

Conditions de Front End Post Save Child

J'essaie de mettre à jour mon formulaire de publication front-end, ce qui me permet de sauvegarder mon terme parent de taxonomie personnalisé, mais je dois maintenant sauvegarder le terme enfant sélectionné.

NHB est pour le quartier qui sont des termes enfants pour la taxonomie de type de ville. Voici mon code en haut pour les champs pertinents -

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

Et -

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

Puis ma liste déroulante pour les conditions pour enfants sur mon formulaire de poste au début -

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

Comment puis-je obtenir cela pour économiser comme terme enfant et aussi sortie? Je produis actuellement le terme parent comme ceci -

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

METTRE À JOUR

En haut de ma page, j'ai les éléments suivants pour enregistrer les conditions. $ city_type est utilisé pour les termes parents de taxonomie "type-ville". $ nhb est pour les termes enfants qui correspondent au terme parent sélectionné/enregistré.

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

  ?>  

La forme -

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

Les termes enfants déroulant sélectionnez -

<?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, je l'ai compris en utilisant la suggestion de @TomMorton.

J'ai dû mettre à jour mon code en mettant à jour les termes -

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

Et ceci pour les mettre -

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

La seule chose que je devais changer dans ma liste déroulante était la suivante: j'utilisais le nom du terme mais je l'ai changé en terme slug -

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

Ensuite, assurez-vous que les termes sont définis, en utilisant -

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

Avec la sauvegarde de termes enfants, je pense que vous pouvez les ajouter au groupe $term_array où réside le parent. Du point de vue d’une publication (ou d’un type de publication personnalisé), l’enregistrement de la taxonomie n’a aucun rapport avec le parent ou l’enfant.

WP Etats du Codex"Pour les termes hiérarchiques (tels que les catégories), vous devez toujours indiquer l'ID plutôt que le nom du terme pour éviter toute confusion s'il peut y avoir un autre enfant du même nom . "Ceci implique que vous devriez pouvoir passer l'ID enfant et qu'il sera classé dans le terme enfant.

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

En ce qui concerne l'affichage des enfants, vous pouvez le faire de deux manières. La première, vous pouvez continuer à parcourir les enfants en fonction de votre 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 . '';
            }
        }
    }
}

Ou vous pouvez passer en revue les enfants après les avoir obtenus du parent (dans le cas où vous pourriez avoir un parent mais que vous avez besoin de ses enfants:

/* 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: Affiche un exemple de fonction de sauvegarde.

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