WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom taxonomy (14 posts)

  1. Code Metic
    Member
    Posted 1 year ago #

    Using this code

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently damaged/corrupted by the forum's parser. ]

    $statuslabels = array(
    	'name' => _x( 'Status', 'taxonomy general name', 'contempo' ),
    	'singular_name' => _x( 'Status', 'taxonomy singular name', 'contempo' ),
    	'search_items' =>  __( 'Search Statuses', 'contempo' ),
    	'popular_items' => __( 'Popular Statuses', 'contempo' ),
    	'all_items' => __( 'All Statuses', 'contempo' ),
    	'parent_item' => null,
    	'parent_item_colon' => null,
    	'edit_item' => __( 'Edit Statuses', 'contempo' ),
    	'update_item' => __( 'Update Statuses', 'contempo' ),
    	'add_new_item' => __( 'Add New Status', 'contempo' ),
    	'new_item_name' => __( 'New Status Name', 'contempo' ),
    	'separate_items_with_commas' => __( 'Separate Statuses with commas', 'contempo' ),
    	'add_or_remove_items' => __( 'Add or remove Status', 'contempo' ),
    	'choose_from_most_used' => __( 'Choose from the most used Statuses', 'contempo' )
    );
    register_taxonomy( 'status', 'listings', array(
    	'hierarchical' => false,
    	'labels' => $statuslabels,
    	'show_ui' => true,
    	'query_var' => true,
    	'rewrite' => array( 'slug' => 'status' ),
    ));
    
    function status() {
    	global $post;
    	global $wp_query;
    	$terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'status', '', ', ', '' ) );
    	echo $terms_as_text;
    }

    I successfully registered custom taxonomy in admin->post sidebar.
    However it generates input text.
    Does anyone knows to turn input['text'] from taxonomy box to dropdown selection.

  2. David Gard
    Member
    Posted 1 year ago #

    Unfortunately there is no built in support for a dropdown Taxonomy, only the Category ('hierarchical' => true) or Tag ('hierarchical' => false) default lay outs.

    It is possible to do though. You need to not display the Taxonomy in any post type, and then add a custom meta box to the new/edit post display. You then use filters to add/remove the taxonomies from the database when the user publishes/updats the post.

    I do have an example that I have used in the past somewhere, but I cannot find it right now. Will post it on Pastebin and let you have the link if I find it.

    As a side note, can you please ensure that you wrap your code in code tags (described under the box where you write the post), and if you code is longer than about 12 lines, please use Pastebin.com and post the link to the code.

  3. David Gard
    Member
    Posted 1 year ago #

    Ok, so I found the code that I am using to do this and pasted it at Pastebin.

    I can see from my code that you do sitll associate the Taxonomy with your Post Type, but that you remove the default, WP generated meta box in the code. In this example, my Post Type was called 'service' and my Taxonomy was called 'sector', but I tend to use constants ('POST_TYPE_SERVICE') and ('TAXONOMY_SECTOR') to display them. Do a search on those 4 terms and replace them with your own terms, and it should hopefully work.

    Thanks.

  4. Code Metic
    Member
    Posted 1 year ago #

    Thanks for your help. Here is the screenshot.

    https://docs.google.com/open?id=0B0WjecGSOzlQd3pJd3owVG1OV28

    But dropdown selects only a single tag for post unlike input text does multiple selection.
    How can i make it look like this.

    https://docs.google.com/open?id=0B0WjecGSOzlQWDUyTHNudDlMZ0E

    Whenever i click the add button the selected option will be included as a part of post tags.

    By the way, you really help me with this problem. Greatly appreciate it.

  5. David Gard
    Member
    Posted 1 year ago #

    Ok, so just so that I am clear -

    • You would like to add a custom taxonomy to a post type.
    • You would like to be able to add multiple terms from the custom
      taxonomy to the post type.
    • You would like for the terms to be displayed in a dropdown menu.

    Assuming all this is true, I believe you just need to add true to the wp_add_object_terms() line, right near the bottom of the save function. This should make it update, rahter than replace the term entries for your custom taxonomy that are associated with that post.

    wp_set_object_terms($post_id, $sector, TAXONOMY_SECTOR, true);

    This of course will not add the 'Add' button. There are two ways you can do this -

    1. Add a button that, when clicked, acts simply as the 'Publish' button.
    2. Add an button that calls an AJAX routine when it is clicked, updating the database in the background.
  6. Code Metic
    Member
    Posted 1 year ago #

    Absolutely. That is exactly what i want. But in the part of wordpress ajax calls, I DO NOT KNOW HOW TO DO IT.

    Would you do it for me. ; )

    Actually im a newbeh! in wordpress. And does not have a deep knowledge on it. Currently im in OJT. I do always study wordpress api and still in learning process.

  7. David Gard
    Member
    Posted 1 year ago #

    Well, not to be to mean, but as someone once said to me on this forum - the best way to learn is to try it yourself. Plus, you are the only one that knows the exact requirements of your project.

    I've given you the code for adding a metabox to your posts screen, so you are 75% of the way there.

    The Codex for WP AJAX should get you started - it has lots of links to useful sites/examples - AJAX « WordPress Codex.

  8. Code Metic
    Member
    Posted 1 year ago #

    How about this

    $dropdown = "\n\n".'<select style="width: 180px; height: 200px;" name="sector" id="sector" multiple="multiple">'."\n";

    I do set multiple attribute. And then.

    wp_set_object_terms($post_id, $_POST['status'], 'status');

  9. Code Metic
    Member
    Posted 1 year ago #

    Just one more time. Does your code accomodates multiple dropdown.
    I cloned it one for "status" and one for "property type". When i click the update button, only "property type" works and "status" stops working.

  10. David Gard
    Member
    Posted 1 year ago #

    I have only ever used it for one taxonomy term dropdown on a post edit screen, so chances are you just need to edit it slightly to differentiate between the two.

    First thing I'd check is the name attribute on the <select>, to ensure that they are unique. I'd then check to make sure I was definatley saving to the correct taxonomies (near the bottom), not using the same on on both.

  11. Code Metic
    Member
    Posted 1 year ago #

    Yap. Ive done that already. Still does not work.

    For Property

    add_action('admin_head', 'remove_property_taxonomy_box');
    function remove_property_taxonomy_box(){
    
    	remove_meta_box('tagsdiv-sector', 'listings', 'side');
    
    }
    
    if(current_user_can('manage_categories')) :
    
    	add_action('admin_menu', 'add_property_dropdown');
    
    	add_action('save_post', 'save_property_data');
    
    endif;
    
    function add_property_dropdown(){
    
    	add_meta_box('property-dropdown', __('Property Type'), 'do_property_dropdown', 'listings', 'side', 'core');
    
    }
    
    function do_property_dropdown(){
    
    	global $post;
    
    	echo '<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="'.wp_create_nonce('taxonomy_pt').'" />';
    
    	$properties = get_terms('property_type', 'hide_empty=0');
    
    	$property_selected = wp_get_object_terms($post->ID, 'property_type');
    
    	$dropdown = "\n\n".'<select style="width: 180px; height: 200px;" name="prop_type[]" id="prop_type" multiple="multiple">'."\n";
    	foreach($properties as $property) :
    		$selected = (!is_wp_error($property_selected) && !empty($property_selected) && !strcmp($property->slug, $property_selected[0]->slug)) ? ' selected="true"' : false;
    		$dropdown.= "\t".'<option id="property-'.$property->term_id.'" value="'.$property->slug.'"'.$selected.'>'.$property->name .'</option>'."\n";
    	endforeach;
    	$dropdown.= '<select name="prop_type" id="prop_type">'."\n\n";
    
    	echo $dropdown;
    
    }
    
    function save_property_data($post_id) {
    
     	if(!wp_verify_nonce($_POST['taxonomy_noncename'], 'taxonomy_pt')) :
        	return false;
      	endif;
    
      	if(!current_user_can('edit_post', $post_id)) :
          	return false;
      	endif;
    
      	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) :
        	return false;
    	endif;
    
    	$post = get_post($post_id);
    	if($post->post_type === 'listings') :
    		wp_set_object_terms($post_id, $_POST['prop_type'], 'property_type', true);
        endif;
    
    	return $sector;
    
    }

    For Status

    add_action('admin_head', 'remove_sectors_taxonomy_box');
    function remove_sectors_taxonomy_box(){
    
    	remove_meta_box('tagsdiv-sector', 'listings', 'side');
    
    }
    
    if(current_user_can('manage_categories')) :
    
    	add_action('admin_menu', 'add_sectors_dropdown');
    
    	add_action('save_post', 'save_sectors_data');
    
    endif;
    
    /**
     * Create the custom 'Sector' meta box
     */
    function add_sectors_dropdown(){
    
    	add_meta_box('sectors-dropdown', __('Status'), 'do_sectors_dropdown', 'listings', 'side', 'core');
    
    }
    
    /**
     * Populate the custom 'Sector' meta box
     */
    function do_sectors_dropdown(){
    
    	global $post;
    
    	/** Create a security nonce for this dorpdown */
    	echo '<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="'.wp_create_nonce('taxonomy_sector').'" />';
    
     	/** Get all sector taxonomy terms */
    	$sectors = get_terms('status', 'hide_empty=0');
    
    	/** Get all of this object's terms that are in the 'sector' taxonomy */
    	$selected_sectors = wp_get_object_terms($post->ID, 'status');
    
    	$dropdown = "\n\n".'<select style="width: 180px; height: 200px;" name="sector[]" id="sector" multiple="multiple">'."\n";
    	foreach($sectors as $sector) :
    		$selected = (!is_wp_error($selected_sectors) && !empty($selected_sectors) && !strcmp($sector->slug, $selected_sectors[0]->slug)) ? ' selected="true"' : false;
    		$dropdown.= "\t".'<option id="sector-'.$sector->term_id.'" value="'.$sector->slug.'"'.$selected.'>'.$sector->name .'</option>'."\n";
    	endforeach;
    	$dropdown.= '<select name="sector" id="sector">'."\n\n";
    
    	echo $dropdown;
    
    }
    
    /**
     * Save the term that the user selects from the custom 'Sector' meta box
     *
     * @param required integer $post_id The id of the post being edited
     * @return false|string $sector The sector that the user selected
     */
    function save_sectors_data($post_id) {
    
    	/** Check the security nonce to ensure we have the proper authorisation  */
     	if(!wp_verify_nonce($_POST['taxonomy_noncename'], 'taxonomy_sector')) :
        	return false;
      	endif;
    
      	/** Ensure that the user has the correct permissions */
      	if(!current_user_can('edit_post', $post_id)) :
          	return false;
      	endif;
    
      	/** Check if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything */
      	if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) :
        	return false;
    	endif;
    
      	/** Now save the data against the correct taxonomy */
    	$post = get_post($post_id);
    	if($post->post_type === 'listings') :
    		wp_set_object_terms($post_id, $_POST['sector'], 'status', true);
        endif;
    
    	return $sector;
    
    }
  12. David Gard
    Member
    Posted 1 year ago #

    Ok, first off, that's make in in to one piece of code - they don't specifiacally have to be seperate, and I find this easier (of course feel free to amend as you see fit). Pastbin

    Next, we'll get rid of the return $sector; line at the very bottom - I don't know why I put that there, it serves no purpose.

    Finally, I notice an error in the code (that came from me). At the end of each <select>, we should be closing it, but we are instead adding another open tag. The lines are directly before echo $dropdown;, and both should be $dropdown.= '</select>'."\n\n";.

  13. Code Metic
    Member
    Posted 1 year ago #

    Thanks. Works fine now.

  14. David Gard
    Member
    Posted 1 year ago #

    Good news.

Topic Closed

This topic has been closed to new replies.

About this Topic