0

I am trying to get the categories of my custom post type in functions.php But it not return any value, when i run this query in any theme file it work fine. Here is my code

function get_destinations(){
   $args = array(
    'type'                     => 'accomodation',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'facilitie',
    'pad_counts'               => false 

); 
 $categories = get_categories($args);$destinations = array();
              foreach ($categories as $cat) { 
                if($cat->cat_name != ''){
                    $destinations[$cat->cat_name] = $cat->cat_name;
                }
          }
return $destinations;
 }

I am using this code to add meta field, now i have to pass the category to select tag which is

$my_meta2->addSelect($prefix.'select_field_id',get_destinations(),array('name'=> 'Select Destination'));

The original code is like this, they pass the value in array.

$my_meta->addSelect($prefix.'select_field_id',array('selectkey1'=>'Select Value1','selectkey2'=>'Select Value2'),array('name'=> 'My select ', 'std'=> array('selectkey2')));

But not getting any value, Any idea where i am wrong. Thanks

4 Answers 4

1

You are using third party code to generate those meta boxes, and I am not familiar with how that code works. In fact, based on your question I am not even sure exactly where the code fails. Your description of the problem is inadequate. I can point out that your code is overly complex. It can be simplified considerably:

function get_destinations(){
  // Using a post type
  $args = array(
    'type'                     => 'book',
    'taxonomy'                 => 'genre',
  ); 
  $categories = get_terms($args); // changed to get_terms()

  if (!is_wp_error()) {
    $destinations = wp_list_pluck($categories,'cat_name');
    /* Convert to key=>value format
       I doubt this is actually necessary and can probably be omitted
    */
    $destinations = array_combine($destinations,$destinations);
    return $destinations;
  }
}
var_dump(get_destinations());
  1. Remove the default arguments. You don't need to repeat those.
  2. Use get_terms() as it returns a proper WP_Error object instead of only a part of one (at least on my install, which I may have broken. I do that periodically). At any rate, get_terms() makes more since as you are not retrieving "categories" but a custom taxonomy.
  3. Verify that you have proper term results before trying to use them
  4. Use wp_list_pluck() to simplify the foreach

As far as Core code, I only see three places where this can fail:

  1. The post type is wrong
  2. The taxonomy is wrong
  3. There are not posts in the type/taxonomy
1
  • Acutally to create taxonomy i used wordpress default code, the 3rd party code is used only for extra meta fields. And the post is correct, taxonomy as well correct, because i get the result when i run on any other page, it not work when i try to run code in functions.php. I will try your code, really thanks for your time and help. Commented Jul 24, 2015 at 18:12
1

It Will work,

$cat_args = array(
                      'parent'  => 0,
                      'hide_empty' =>0,
                      'order'    => 'ASC',
                   );
    $categories = get_categories($cat_args);
    foreach($categories as $category){
       echo get_cat_name($category->term_id); 
    }
2
  • Can you explain a little as how it works?
    – bravokeyl
    Commented Dec 21, 2016 at 12:00
  • sure, get_categories hook i was passing parameter parent = > 0 mean all categories(main,sub) hide_empty mean without post be some categories also took.
    – Samyappa
    Commented Dec 21, 2016 at 13:54
0

Check the Parameters section in the WordPress Codex for get_categories.

https://codex.wordpress.org/Function_Reference/get_categories#Parameters

The type parameter only accepts 2 values (post & link) and you are trying to use accomodation.

What you are probably wanting is get_terms.

https://codex.wordpress.org/Function_Reference/get_terms

So you will use this instead of get_categories.

$categories = get_terms('facilitie');

For what you are trying to do you probably don't need any $args array.

Then in your foreach you will just want to change $cat->cat_name to $cat->name.

5
  • But in this how do i specify which terms i need to retrieve, because i have 3 to 4 type of custom posts, and everyone have 2 to 3 type of taxonomy. So i have to more specify which terms i need. Commented Jul 24, 2015 at 18:20
  • I try your suggested code, but it not work either, i show values if i add code in any template, but it not show any value in fucntions.php file. Commented Jul 24, 2015 at 18:34
  • First, you can get multiple terms by using an array with the name of each term, like $categories = get_terms(array('term1', 'term2'));. Second, what is not working when you use this? Are you getting an error? Where is your meta field code at? If that is in your functions.php file (where it should be) then there shouldn't be any problems.
    – Josh Riser
    Commented Jul 24, 2015 at 18:47
  • All code are in functions.php I just read a article here It says i use add_action but still not working for me really really upset. Commented Jul 24, 2015 at 18:53
  • here is my new code. function retrieve_my_terms(){ $cats = array(); global $terms; $categories = get_terms( 'destination', 'orderby=count&hide_empty=0' ); foreach($categories as $cat){ $cats[$cat->name] = $cat->name; } return $cats; } add_action('init', 'retrieve_my_terms', 9999); print_r(retrieve_my_terms()); When i call this function in templete it show values but not in functions.php which i need Commented Jul 24, 2015 at 18:57
0
function get_cat_val(){

    $taxonomies = array('YOUR_CAT_SLUG');

    $args = array(
        'order'             => 'ASC',
        'hide_empty'        => false,
        'hierarchical'      => true 
    ); 

    $terms = get_terms($taxonomies, $args);

    print_r($terms);

}

or

global $post;
$term_class = array();

$terms = get_the_terms( $post->ID, 'YOUR CAT SLUG' );

if ( $terms && ! is_wp_error( $terms ) ) {

    foreach ( $terms as $term ) {
        $term_class[] = $term->slug;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.