Search code examples
wordpresstaxonomypage-title

Get taxonomy name as Wordpress page title


I am trying to save the selected taxonomy as a part of a page title. I'm fairly new to coding (1 month ago) so basically, my head is about to explode right now. Hope someone can help me out. Thanks for you help.

            // CRED SAVE DATA HOOK
            add_action('cred_save_data', 'my_save_data_action',10,2);
            function my_save_data_action($post_id, $form_data)
            {
                // if a specific form
                if ($form_data['id']==9) 
                {
                    {
                    if (isset($_POST["property-new-name"])) 
                    {
                        $terms = $_POST['property-new-name'];
                        $taxonomy = 'property-name';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["route"])) 
                    {
                        $terms = $_POST['route'];
                        $taxonomy = 'street';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["township"])) 
                    {
                        $terms = $_POST['township'];
                        $taxonomy = 'township';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["locality"])) 
                    {
                        $terms = $_POST['locality'];
                        $taxonomy = 'city';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["postal_code"])) 
                    {
                        $terms = $_POST['postal_code'];
                        $taxonomy = 'postal-code';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["administrative_area_level_1"])) 
                    {
                        $terms = $_POST['administrative_area_level_1'];
                        $taxonomy = 'state';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["country"])) 
                    {
                        $terms = $_POST['country'];
                        $taxonomy = 'country';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }
                    if (isset($_POST["country_short"])) 
                    {
                        $terms = $_POST['country_short'];
                        $taxonomy = 'country-code';
                        wp_set_object_terms( $post_id, $terms, $taxonomy, true );
                    }


                    {

                        $taxonomy = 'property-name';
                        $term_list = wp_get_post_terms( $post->ID, $taxonomy, array("fields" => "names"));
                        $term = $term_list[0]; // getting first term, if there are multiple terms
                        $proname = $term;

                        $contract = get_post_meta( get_the_ID(), "wpcf-contract", true );
                        $title = $proname ." for " . $contract . " " . $_POST["township"]; 
                        // Update the post into the database
                        $my_post = array(
                            'ID'           => $post_id,
                            'post_title' => $title
                        );
                        $my_post['post_status'] = $_REQUEST['post_status'];
                        wp_update_post( $my_post ); 
                 } 
             }

            }
            }

Solution

  • I got it. Here is the code:

            $list = wp_get_post_terms( get_the_ID(), 'property-name', array("fields" => "names"));
            $proname = $list[0];
    

    Thanks @chiragswadia