Function Reference: geodir_set_postcat_structure

Summary

Set post category structure based on given parameters.

Package

GeoDirectory

Parameters

$post_id
(int) (required) The post ID.

Default: None
$taxonomy
(string) (required) Name of the taxonomy e. g place_category.

Default: None
$default_cat
(string|int) (optional) The default category ID.

Default: None
$category_str
(array|string) (optional) Blank string if no categories. Else array of categories to set.

Default: None

Change Log

Since: 1.0.0

Source File

geodir_set_postcat_structure() is located in geodirectory-functions/post_functions.php [Line: 20]

Source Code

function geodir_set_postcat_structure($post_id, $taxonomy, $default_cat = '', $category_str = '')
{

    $post_cat_ids = geodir_get_post_meta($post_id, $taxonomy);
    if (!empty($post_cat_ids))
        $post_cat_array = explode(",", trim($post_cat_ids, ","));

    if (!isset($default_cat) || empty($default_cat)) {
        $default_cat = isset($post_cat_array[0]) ? $post_cat_array[0] : '';
    }else{
        if(!is_int($default_cat)){
            $category = get_term_by('name', $default_cat, $taxonomy);
            if(isset($category->term_id)){
                $default_cat =  $category->term_id;
            }
        }

    }


    geodir_save_post_meta($post_id, 'default_category', $default_cat);

    if (isset($category_str) && empty($category_str)) {

        $post_cat_str = '';
        $post_categories = array();
        if (isset($post_cat_array) && is_array($post_cat_array) && !empty($post_cat_array)) {
            $post_cat_str = implode(",y:#", $post_cat_array);
            $post_cat_str .= ",y:";
            $post_cat_str = substr_replace($post_cat_str, ',y,d:', strpos($post_cat_str, ',y:'), strlen(',y:'));
        }
        $post_categories[$taxonomy] = $post_cat_str;
        $category_str = $post_categories;
    }

    $change_cat_str = $category_str[$taxonomy];

    $default_pos = strpos($change_cat_str, 'd:');

    if ($default_pos === false) {

        $change_cat_str = str_replace($default_cat . ',y:', $default_cat . ',y,d:', $change_cat_str);

    }

    $category_str[$taxonomy] = $change_cat_str;

    update_post_meta($post_id, 'post_categories', $category_str);

}