Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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); 
} 
?>
share|improve this question
    
<?php global $current_user; get_currentuserinfo(); $authorcategory = get_the_author_meta('authorcategory', $current_user->ID); $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'texonomy' ) ); if( is_term( $authorcategory, 'blogs', 'post_type=networks' && $term->slug == $authorcategory) ){ print 'Term exists'; }else{ print 'Term does not exist'; } ?> I get Term Exist message with this code. Now how do I get the term ID and include it in the dropdown list –  puanthanh Mar 8 '11 at 12:01
    
I think I can work it out if someone tell me this-- If term exist get the the ID???? –  puanthanh Mar 8 '11 at 12:11
add comment

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:

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..

share|improve this answer
    
Now i read that back, i'm not sure the term_exists check is even necessary, if $myterm has data, then term_exists will naturally be true to. –  t31os Mar 8 '11 at 12:20
    
@t31os Your code seems to be correct but it doesn't show the term nor it is selected . I give a existing term $authorcategory = 'business'; still than no sign –  puanthanh Mar 8 '11 at 14:36
    
What I want is only the $authorcategory and if possible block others term from showing –  puanthanh Mar 8 '11 at 14:41
    
Please give related tuts and tips links –  puanthanh Mar 8 '11 at 14:43
    
Not sure i follow, are you saying you want a dropdown(select) box with only one item in it? –  t31os Mar 8 '11 at 14:44
show 30 more comments

This work for me

<p>
 <?php 
  global $current_user;
  get_currentuserinfo();
  $authorcategory = get_user_meta( $current_user->ID, 'authorcategory', true );
  $user_term = get_term_by( 'slug', $authorcategory, 'blogs');
  $mycat = $user_term->slug;
  $cats_array = get_categories(array('child_of=$mycat','taxonomy' => 'blogs'));
 ?>
</p>
<p>
 <strong>Is <?php echo $mycat; ?> your Blog Name? Make it sure before posting </strong>
 <select style=&quot;display:none;&quot; id=&quot;cat&quot; name=&quot;cat&quot; class=&quot;input&quot;>
  <?php foreach ( $cats_array as $category ): ?>
  <?php if ($category->cat_name == $mycat ): ?>
  <option value=&quot;<?php echo $category->cat_ID; ?>&quot;><?php echo $category->cat_name; ?></option>
  <?php endif; ?>   
  <?php endforeach; ?> 
 </select>
</p>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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