Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Assign a custom post type terms, to Users when writing new posts WordPress

  • SOLVED

Hi!

I have a <em>Custom Post Type</em> called: "Last Minute"

In that custom post type "Last Minute" i've created a custom taxonomy with 4 terms

The terms are "Rome", "Torino", "Florence" and "Venezia"

The system work fine! But i want to assign a specific term to an user

Example i want:

<strong>user1 ---> assign a category called "Rome" and when user login and write post AUTOMATICALLY this post is assigned in the category "Rome"</strong>

but i have for example more authors….

In my case, every user as set with the role of "Author"

Thanks in advance!

Answers (1)

2011-11-04

Luis Abarca answers:

I made this before in this way

I create a extra field to the user, and show list of categories, then select a category and add it to the user meta.

When the user save a post, on save post action, i add this category to the post.


christian pucci comments:

nice system but is possible to do that without the user select categories?

For example only Admin can assign the category to the user


Luis Abarca comments:

The code for user profile


// add select to user profile
add_action( 'show_user_profile', 'my_user_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_user_extra_profile_fields' );

function my_user_extra_profile_fields( $user )
{
?>
<h3>Assigned category</h3>

<table class="form-table">
<tr>
<th><label for="_mycategory">Category</label></th>

<td>
<?php wp_dropdown_categories('show_option_none=- Select -&name=_mycategory&id=_mycategory&selected=' . esc_attr( get_the_author_meta('_mycategory', $user->ID) ) ) ?>
<br />
<span class="description">Select a category.</span>
</td>
</tr>
</table>
<?php
}

// }}}
// {{{

add_action( 'personal_options_update', 'my_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_user_profile_fields' );

function my_save_extra_user_profile_fields( $user_id )
{
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}

if ( current_user_can('manage_options') ) {
$item = '';

if ( isset($_POST['_mycategory']) && !empty($_POST['_mycategory']) ) {
$item = intval($_POST['_mycategory']);
}

update_user_meta( $user_id, '_mycategory', $item );
}
}


Luis Abarca comments:

Notice: you have to add the name of your custom taxonomy to the wp_dropdown_categories, because you didn't mention it.


<?php wp_dropdown_categories('<strong>taxonomy=mycustomtaxonomy</strong>&show_option_none=- Select -&name=_mycategory&id=_mycategory&selected=' . esc_attr( get_the_author_meta('_mycategory', $user->ID) ) ) ?>


christian pucci comments:

Nice solution but that function return the category for the blog and not the custom category i have assigned to "Last Minute"

in your code is: wp_dropdown_categories

but i want dropdown list of terms

This is one of my terms i have added on Taxonomy "Last Minute", example:

wp_insert_term( 'Rome', 'luogolastminute');
wp_insert_term( 'Florence', 'luogolastminute');


"luogolastminute" is the name of my custom Taxonomy

Your code is very interesting!


Luis Abarca comments:

For the save post


add_action('save_post', 'my_save_postdata', 10, 2);

function my_save_postdata( $post_id )
{
$post = get_post( $post_id );
$term_id = get_the_author_meta('_mycategory', $post->post_author );
$term = get_term( $cat_id, 'mytaxonomyname');

wp_set_object_terms( $post_id, $term->slug, $term->taxonomy );

}


Luis Abarca comments:

Hi christian pucci, i put everything in one place in pastebin, http://pastebin.com/Hp8thjwm

Its the same code


<?php
// add a select field to user profile
add_action( 'show_user_profile', 'my_user_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_user_extra_profile_fields' );

function my_user_extra_profile_fields( $user )
{
?>
<h3>Assigned category</h3>
<table class="form-table">
<tr>
<th><label for="_mycategory">Category</label></th>
<td>
<?php wp_dropdown_categories('taxonomy=luogolastminute&show_option_none=- Select -&name=_mycategory&id=_mycategory&selected=' . esc_attr( get_the_author_meta('_mycategory', $user->ID) ) ) ?>
<br />
<span class="description">Select a category.</span>
</td>
</tr>
</table>
<?php
}

// }}}
// {{{

add_action( 'personal_options_update', 'my_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_user_profile_fields' );

function my_save_extra_user_profile_fields( $user_id )
{
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}

if ( current_user_can('manage_options') ) {
$item = '';

if ( isset($_POST['_mycategory']) && !empty($_POST['_mycategory']) ) {
$item = intval($_POST['_mycategory']);
}

update_user_meta( $user_id, '_mycategory', $item );
}
}

/*
* On save post
*
*/
add_action('save_post', 'my_save_postdata', 10, 2);

function my_save_postdata( $post_id )

{
// get the post from the post ID, to get the author ID
$post = get_post( $post_id );

// get the ID from the user meta
$term_id = get_the_author_meta('_mycategory', $post->post_author );

// get the term object
$term = get_term( $cat_id, 'luogolastminute');

// assign the term to the post
wp_set_object_terms( $post_id, $term->slug, $term->taxonomy );
}
?>


christian pucci comments:

ok :)

We are near the solution but the select in the user profile is empty and any categories is displayed.

[[LINK href="http://d.pr/Rwc2"]]This is what i see[[/LINK]]


christian pucci comments:

Luis, this is my taxonomy code, i think help you and my to understand:

<?php
// LUOGHI PER LAST MINUTE
add_action( 'init', 'luoghi_last_minute_taxonomies', 0 );

function luoghi_last_minute_taxonomies()
{
$labels = array(
'name' => _x( 'Luoghi Last Minute', 'taxonomy general name' ),
'singular_name' => _x( 'Luoghi Last Minute', 'taxonomy singular name' ),
'search_items' => __( 'Cerca Luoghi Last Minute' ),
'all_items' => __( 'Tutti i Luoghi Last Minute' ),
'parent_item' => __( 'Genitore Last Minute' ),
'parent_item_colon' => __( 'Genitore Last Minute:' ),
'edit_item' => __( 'Modifica Last Minute' ),
'update_item' => __( 'Aggiorna Last Minute' ),
'add_new_item' => __( 'Inserisci Nuovo Luogo Last Minute' ),
'new_item_name' => __( 'Nuovo Luogo Last Minute' ),
'menu_name' => __( 'Luoghi Last Minute' ),
);

register_taxonomy('luogolastminute',array('lastminute'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'last-minute', 'hierarchical' => true ),
));
wp_insert_term( 'Rome', 'luogolastminute');
wp_insert_term( 'Torino', 'luogolastminute');
wp_insert_term( 'Florence', 'luogolastminute');
wp_insert_term( 'Venezia', 'luogolastminute');
}
?>


Luis Abarca comments:

Yep, i updated the code on pastebin http://pastebin.com/Hp8thjwm


Luis Abarca comments:

Just a question, the categories, already had posts assigned ?? or are empty ??


Luis Abarca comments:

if are empty doesn't appear, i'm adding the hide_empty => 0 to the wp_dropdown_categories to shows even empty terms


christian pucci comments:

Thanks for your patience Luis :)

No, the categories not had any posts assigned.

To more understand this is the code for my custom post type:

<?php
//CUSTOM POST TYPE PER UTENTE FINALE - OFFERTE LAST MINUTE
add_action('init', 'last_minute_custom_init');
function last_minute_custom_init()
{
$labels = array(
'name' => _x('Last Minute', 'post type general name'),
'singular_name' => _x('Last Minute', 'post type singular name'),
'add_new' => _x('Inserisci Last Minute', 'schedahotel'),
'add_new_item' => __('Inserisci Nuovo Last Minute'),
'edit_item' => __('Modifica Last Minute'),
'new_item' => __('Nuovo Last Minute'),
'all_items' => __('Tutti i Last Minute'),
'view_item' => __('Guarda il Last Minute'),
'search_items' => __('Cerca Last Minute'),
'not_found' => __('Nessun Last Minute'),
'not_found_in_trash' => __('Nessun Last Minute nel Cestino'),
'parent_item_colon' => '',
'menu_name' => 'Last Minute'

);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'menu_icon' => get_bloginfo('template_directory') . '/images/icon_last_minute.png',
'supports' => array('title','editor')
);
register_post_type('lastminute',$args);
}
?>


Luis Abarca comments:

I added the code to show even empty terms, check this http://pastebin.com/Hp8thjwm


<?php
// add a select field to user profile
add_action( 'show_user_profile', 'my_user_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_user_extra_profile_fields' );

function my_user_extra_profile_fields( $user )
{
$args = array(
'taxonomy' => 'luogolastminute',
'show_option_none' => '- Select -',
'name' => '_mycategory',
'id' => '_mycategory',
'selected' => esc_attr( get_the_author_meta('_mycategory', $user->ID) ),
'hide_empty' => 0
);

?>
<h3>Assigned category</h3>
<table class="form-table">
<tr>
<th><label for="_mycategory">Category</label></th>
<td>
<?php wp_dropdown_categories( $args ) ?>
<br />
<span class="description">Select a category.</span>
</td>
</tr>
</table>
<?php
}

// }}}
// {{{

add_action( 'personal_options_update', 'my_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_user_profile_fields' );

function my_save_extra_user_profile_fields( $user_id )
{
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}

if ( current_user_can('manage_options') ) {
$item = '';

if ( isset($_POST['_mycategory']) && !empty($_POST['_mycategory']) ) {
$item = intval($_POST['_mycategory']);
}

update_user_meta( $user_id, '_mycategory', $item );
}
}

/*
* On save post
*
*/
add_action('save_post', 'my_save_postdata', 10, 2);

function my_save_postdata( $post_id )

{
// get the post from the post ID, to get the author ID
$post = get_post( $post_id );

// get the ID from the user meta
$term_id = get_the_author_meta('_mycategory', $post->post_author );

// get the term object
$term = get_term( $cat_id, 'luogolastminute');

// assign the term to the post
wp_set_object_terms( $post_id, $term->slug, $term->taxonomy );
}
?>


christian pucci comments:

ok i tryed your last code and the dropdown work fine and save fine, but when i go to "luoghi last minute" (the categories for custom post type...) those are empty...


christian pucci comments:

.... for example custom category called "Rome" are empty but i have selected "Rome" in the user profile...


Luis Abarca comments:

are you or your user posting content with the assigned category ?? or the content existed before the changes ?? in that case the author needs to re-save theris posts to add the terms to the post


christian pucci comments:

No Luis, i don't want the user select the category in this case for example: "Rome"

I'm the Admin and i want to assign one of my custom category to an user!

When the user login, he can write only in the Title textarea and in the Editor textarea, but he can't select the category because before i've assigned to he


Luis Abarca comments:

Yep, its the way it works, the user doesn't needs to select a category, because on save_post we add it to the post, can you send me login details via PM to check whats wrong ?? this is the code i'm using in a web site and it's working fine.


christian pucci comments:

ok i send to you the details! thanks Luis! :)


Luis Abarca comments:

Its now working, this is the final solution http://pastebin.com/Hp8thjwm

I was using $cat_id and should be $term_id in line 71


christian pucci comments:

Perfect Luis now working fine! Many many thanks!!!