0

I'm attempting to show _all tags on the editing screen on a custom post type (products). Adapting these instructions

I've created a simple plugin as follows:

add_action( 'wp_ajax_get-tagcloud', 'ajax_tag_cloud_wpse_99497', 1 );
function ajax_tag_cloud_wpse_99497(){

   $tags = get_terms( $taxonomy, array( 
   'number' => 999, 
   'orderby' => 'count', 
   'order' => 'DESC', 
   'taxonomy'  => 'Sizes',
   ) ); 
}

which doesn't work. On the custom post type, I'm still only seeing the latest / most popular 45 tags. Am I doing something wrong here? I'm completely stumped.

EDIT: Here's the entire plugin as I currently have it coded, and it still isn't working.

/*  
Plugin Name: Display All Tags
Description: When "Choose from the most used tags" is clicked, show all tags rather than most recent 45. 
Version: 1.0
Author: Michael Champlin
Author URI: http://mjchamplin.com
*/

add_action( 'wp_ajax_get-tagcloud', 'ajax_tag_cloud_wpse_99497', 1 );

function ajax_tag_cloud_wpse_99497() 
{

$tags = get_terms( 'sizes', array( 
    'number' => 999, 
    'orderby' => 'count', 
    'order' => 'DESC', 

) );

}
2

I needed something like this for a client site, they wanted to add tags to all their products, but I didn't want them just making up new tags, I wanted to give them a list of tags to start with.

vancoder's answer provided the key I needed to get this done: the 'hide_empty' attribute.

What makes the code in the original question not work is it's incomplete. Because you are overriding a WP function, you need to replace the entire function with your custom version, which must handle all the functionality of the original. The original function is found in wp-admin/includes/ajax-actions.php (line 836 in version 4.3) so that is copied and modified to create the overriding function:

add_action( 'wp_ajax_get-tagcloud', 'ti_theme_tag_cloud', 1);

function ti_theme_tag_cloud() 
{

 if ( ! isset( $_POST['tax'] ) ) {
    wp_die( 0 );
 }

 $taxonomy = sanitize_key( $_POST['tax'] );
 $tax = get_taxonomy( $taxonomy );
 if ( ! $tax ) {
    wp_die( 0 );
 }

 if ( ! current_user_can( $tax->cap->assign_terms ) ) {
    wp_die( -1 );
 }

 $term_params = array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' );
 switch ($taxonomy) {
  case 'product_tag':
    $term_params['hide_empty'] = false;
   break;
 }
 $tags = get_terms( $taxonomy, $term_params );


 if ( empty( $tags ) )
    wp_die( $tax->labels->not_found );

 if ( is_wp_error( $tags ) )
    wp_die( $tags->get_error_message() );

 foreach ( $tags as $key => $tag ) {
    $tags[ $key ]->link = '#';
    $tags[ $key ]->id = $tag->term_id;
 }

 // We need raw tag names here, so don't filter the output
 $return = wp_generate_tag_cloud( $tags, array('filter' => 0) );

 if ( empty($return) )
    wp_die( 0 );

 echo $return;

 wp_die();

}

This overriding function adds an opportunity to change the tag cloud configuration based on the incoming taxonomy without affecting other taxonomies.

As an added refinement, I changed one of the labels in the 'product_tag' taxonomy:

'choose_from_most_used' => 'Choose Tags',

So that the user knows they are just getting a list of all the tags.

1

By default, get_terms will only return those tags that have some posts attached to them already.

Try:

$tags = get_terms( 'sizes', array( 
   'number' => 999, 
   'orderby' => 'count', 
   'order' => 'DESC', 
   'hide_empty' => false,
   ) );

The hide_empty parameter will override this behaviour.

EDIT: I removed the 'taxonomy' parameter for your args, as it is not valid: http://codex.wordpress.org/Function_Reference/get_terms

EDIT: I hard-coded your taxonomy name (which I'm hoping is all lower case).

  • This makes sense, thanks! Unfortunately it isn't working -- could it be because it's a custom taxonomy on a custom post type? – Michael C. Dec 10 '14 at 23:58
  • 1
    It shouldn't. I didn't read all of the article you cited, but I wouldn't trust the advice of anyone who recommends hacking WP core to be honest. – vancoder Dec 11 '14 at 0:09
  • See my edit - this rogue param may have caused problems. – vancoder Dec 11 '14 at 0:15
  • Saw that, and removed it -- still no dice. – Michael C. Dec 11 '14 at 0:26
  • 1
    If your updated code doesn't work when you use hide_empty (which isn't included there), then I'm out of ideas. – vancoder Dec 11 '14 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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