PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Posting Using Category Slug

  1. I'm using this code below to post into specific categories using the category ID… and it works great… what I'm wondering is if there's a way to make it grab and use the category "slug" instead?

    I have, literally, over 1,000 categories. And when I make a new one (or a new set of them) referencing the category ID takes forever. If I could simply type in the slug it would make life oh-so-easy!

    Here's the code...

    /* Gravity Forms Posting */
    
    add_action('gform_post_submission', 'gform_add_greenmy_home', 10, 2);
    function gform_add_greenmy_home($entry, $form){
    
        if(!rgar($entry, 'post_id'))
            return;
    
        $post_id = rgar($entry, 'post_id');
        $valid_cat_ids = get_greenmy_home_ids();
        $cat_ids = array();
    
        foreach($form['fields'] as $field){
    
            if(strpos($field['cssClass'], 'add_greenmy_home') === false)
                continue;
    
            foreach($entry as $key => $value){
    
                if(intval($key) != $field['id'] || !in_array($value, $valid_cat_ids))
                    continue;
    
                $cat_ids[] = (int) $value;
    
            }
    
        }
    
        wp_set_object_terms($post_id, $cat_ids, 'greenmy_home', false);
    
    }
    
    function get_greenmy_home_ids(){
    
        $categories = get_terms('greenmy_home', 'hide_empty=0');
        $cat_ids = array();
    
        foreach($categories as $category)
            $cat_ids[] = $category->term_id;
    
        return $cat_ids;
    }
    
    function print_greenmy_home($array){
        echo '<pre>';
        print_r($array);
        echo '</pre>';
    }
    Posted 12 years ago on Monday February 20, 2012 | Permalink
  2. Hi Noel,

    It doesn't look like it'd take much to convert this to slugs rather than IDs. This bit from the get_greenmy_home_ids() function would need to be updated like so:

    [php]
    foreach($categories as $category)
        $cat_ids[] = $category->slug;

    You might consider renaming the variables through out to indicate that you are using slugs now (ie $cat_ids would be come $cat_slugs).

    You would also want to make sure you update the values of your custom category field in GF to use the slugs as well.

    Posted 12 years ago on Monday February 20, 2012 | Permalink
  3. I had tried that...

    Is it because all of the categories are in custom taxonomies?
    I'm betting yes.

    Posted 12 years ago on Monday February 20, 2012 | Permalink
  4. Try using print_r() to echo out the $categories variable to see if you're successfully retrieving any categories. If so, check and make sure the slug property exists for these categories.

    Posted 12 years ago on Tuesday February 21, 2012 | Permalink