wordpress get taxonomy list of custom post type

user3752276 picture user3752276 · Aug 29, 2014 · Viewed 27.4k times · Source

I am using "Video" theme for my wordpress website. In this theme, videos post type and "videoscategory" taxonomy are defined. Here is the register code for taxonomy:

add_action("init", "custom_posttype_menu_wp_admin1");
function custom_posttype_menu_wp_admin1()
{

 register_post_type('videos', 
            array('label' => __('Videos','templatic'),
                    'labels' => array(  'name' => __('Video','templatic'),
                                'singular_name' => __('Video','templatic'),
                                'add_new' => __('Add Video','templatic'),
                                'add_new_item'=> __('Add New Video','templatic'),
                                'edit' => __('Edit','templatic'),
                                'edit_item'=> __('Edit Video','templatic'),
                                'new_item' => __('New Video','templatic'),
                                'view_item' => __('View Video','templatic'),
                                'search_items' => __('Search Videos','templatic'),
                                'not_found' => __('No Videos found','templatic'),
                                'not_found_in_trash'=> __('No Videos found in trash','templatic')   
                               ),
                    'public'            => true,
                    'can_export'        => true,
                    'show_ui'           => true, // UI in admin panel
                    '_builtin'          => false, // It's a custom post type, not built in
                    '_edit_link'        => 'post.php?post=%d',
                    'capability_type'   => 'post',
                    'menu_icon'         => get_bloginfo('template_url').'/images/favicon.ico',
                    'hierarchical'      => false,
                    'rewrite'           => array("slug" => "videos"), // Permalinks
                    'query_var'         => "videos", // This goes to the WP_Query schema
                    'supports'          => array(   'title',
                                                    'author', 
                                                    'excerpt',
                                                    'thumbnail',
                                                    'comments',
                                                    'editor', 
                                                    'trackbacks',
                                                    'custom-fields',
                                                    'revisions') ,
                    'show_in_nav_menus' => true ,
                    'taxonomies'        => array('videoscategory','videostags')
                )
            );
// Register custom taxonomy
register_taxonomy(  "videoscategory", 
            array(  "videos"    ), 
            array ( "hierarchical"=> true, 
                    "label"=> "Category", 
                    'labels'=> array(   'name' => __('Category','templatic'),
                             'singular_name'=> __('Category','templatic'),
                             'search_items'=> __('Search Category','templatic'),
                             'popular_items' => __('Popular Category','templatic'),
                              'all_items'=> __('All Category','templatic'),
                              'parent_item' => __('Parent Category','templatic'),
                              'parent_item_colon' => __('Parent Category:','templatic'),
                              'edit_item' => __('Edit Category','templatic'),
                              'update_item'=> __('Update Category','templatic'),
                              'add_new_item'=> __('Add New Category','templatic'),
                              'new_item_name'=> __('New Make Category','templatic') ), 
                              'public'=> true,
                              'show_ui' => true,
                              'query_var'=> true,
                              "rewrite" => true 
 )
            );

}

I added some categories under videoscategory taxonomy. Now i want to get all the categories that belongs to videoscategory. I googled the whole day, and I use

$tax_terms =get_terms('videoscategory');

but get empty array. Does anyone know what is the problem? thanks.

Answer

deemi-D-nadeem picture deemi-D-nadeem · Aug 29, 2014

Here is use this code, you definitely find your solution

$cat_args = array(
    'orderby'       => 'term_id', 
    'order'         => 'ASC',
    'hide_empty'    => true, 
);

$terms = get_terms('videoscategory', $cat_args);

    foreach($terms as $taxonomy){
         $term_slug = $taxonomy->slug;

    $tax_post_args = array(
          'post_type' => 'videos',
          'posts_per_page' => 999,
          'order' => 'ASC',
          'tax_query' => array(
                array(
                     'taxonomy' => 'videoscategory',
                     'field' => 'slug',
                     'terms' => '$term_slug'
                )
           )
    );

    $tax_post_qry = new WP_Query($tax_post_args);

    if($tax_post_qry->have_posts()) :
         while($tax_post_qry->have_posts()) :
                $tax_post_qry->the_post();

                the_title();

          endwhile;

    else :
          echo "No posts";
    endif;
} //end foreach loop