PHP Developer

Tuesday, 3 May 2016

How to assign category or “custom taxonomy” category to post from front end



How to assign category or “custom taxonomy” category to post from front end


<?php
$custom_texonmy = 'services_categories';  // Custom texonomy type slug
$job_category = 'Access Covers & Gratings';  //Category name
$post_id = $post_id; //Post id for which assign above category
$cat = get_term_by('name',htmlspecialchars($job_category),$custom_texonmy);  //get category by name
if($cat){
    $cat_ID = $cat->term_id;  // term_id of category
    $job_type = $cat->slug;  //Slug of category
}
if ($job_type) {
    $services_categories = wp_set_object_terms($post_id, $job_type, $custom_texonmy );  //This code assign category of custom texonomy to post from front end
}

?>

No comments:

Post a Comment