Creating Custom wp_dropdown_categories

I have been working for many days on this. I want to assigned category to author. I have googled, found some tips and plugins but doesn’t work with WordPress 3.1. I just come up with my own idea.

I as admin will create a category for an author than define or put category slug name in their respective profile meta field.

I am using a Custom Post type name ‘networks and taxonomy = blogs

Now I am trying to include only the profile meta field value (I said above) in the wp dropdown categories as default and hide it in my custom posting form.

The cat ID and Name are correct when I echo it but it does not include in the drop list. Can anyone help me?

<?php
global $current_user;

get_currentuserinfo();

$authorcategory = get_the_author_meta('authorcategory', $current_user->ID);
$myterm = get_term_by( 'slug', $authorcategory, 'blogs');

if ( is_term( $authorcategory, 'blogs' && $authorcategory == $myterm ) ) {
    $my_cat_id = get_cat_id($authorcategory);
    $my_cat_name = get_cat_name($my_cat_id);
    $args = array(
        'orderby' => 'name',
        'order' => 'ASC',
        'show_last_update' => 0,
        'style' => 'list',
        'show_count' => 0,
        'hide_empty' => 0,
        'include' => $my_cat_name,
        'hierarchical' => true,
        'title_li' => __( 'Categories' ),
        'show_option_none' => __('No categories'),
        'number' => NULL,
        'taxonomy' => 'blogs'
    );
    wp_dropdown_categories($args); 
} 
?>

2 Answers
2

You seem to be mixing up wp_list_categories arguments with wp_dropdown_categories, so i’ve removed them from the code that follows, i’ve also assumed the include argument is suppose to refer to the currently selected item.

For ref:

  • wp_dropdown_categories
  • wp_list_categories

Suggested code:

global $current_user;

get_currentuserinfo();

$authorcategory = get_user_meta( $current_user->ID, 'authorcategory', true );
$user_term = get_term_by( 'slug', $authorcategory, 'blogs');

// Uncomment the two forward slashes before print for debug
// print '<pre>';print_r( $authorcategory );print '</pre>';

if( $user_term ) {
    $args = array(
        'orderby' => 'name',
        'order' => 'ASC',
        'show_last_update' => 0,
        'show_count' => 0,
        'hide_empty' => 0,
        'selected' => 0,
        'child_of' => $user_term->ID,
        'hierarchical' => true,
        'title_li' => __( 'Categories' ),
        'show_option_none' => __('No categories'),
        'taxonomy' => 'blogs'
    );
    wp_dropdown_categories($args); 
} 

You didn’t need either of these two lines..

$my_cat_id = get_cat_id($authorcategory);
$my_cat_name = get_cat_name($my_cat_id);

You already have a complete object of that term in $myterm after you call get_term_by.

Hope that helps..

Leave a Comment