Function Reference: gdsc_manage_category_choice

Summary

Get the category id from category name/slug.

Parameters

$post_type
(string) (required) Post type.

Default: None
$category
(string) (required) Post category.

Default: None

Return Values

(int)
  • Term id.

Change Log

Since: 1.0.0

Source File

gdsc_manage_category_choice() is located in geodirectory-functions/shortcode_functions.php [Line: 246]

Source Code

function gdsc_manage_category_choice($post_type, $category)
{
    if (0 == $category || '' == $category) {
        return '';
    }

    if (!(gdsc_is_post_type_valid($post_type))) {
        return '';
    }

    $taxonomies = geodir_get_taxonomies($post_type);

    $categories = get_terms(array('taxonomy' => $taxonomies[0]));

    $cat_id = 0;

    foreach ($categories as $cat) {
        if (is_numeric($category)) {
            if (absint($category) == $cat->term_id) {
                $cat_id = $cat->term_id;
                break;
            }
        } else {
            if ($category == $cat->slug) {
                $cat_id = $cat->term_id;
                break;
            }

            if ($category == $cat->name) {
                $cat_id = $cat->term_id;
                break;
            }
        }
    }

    return $cat_id;
}