Support

Account

Home Forums Front-end Issues Recall the tags that have a connection to the posts with field taxonomy

Solved

Recall the tags that have a connection to the posts with field taxonomy

  • Hello, until now I have avoided asking for support as you have a perfect documentation !!
    But now I need a helping hand, because I do not find a solution:
    I have a custom post type called “innovators”, within the post “innovators” I created a custom fields of taxonomy type (which makes reference to the custom taxonomies).
    Now I’d need only recall the tags that have a connection to the posts “innovators” and number of connections.
    Is there a way to do this? Keep in mind that the custom field taxonomy is multiple selection.
    Thank you

  • Hi @maxdel85

    Could you please explain your goal in more detail? Maybe you can share a screenshot with notes on it?

    Thanks!

  • You and thank you.
    I created a taxonomy field, print 1.
    I connected the custom post type “innovators” to the taxonomy field.
    So any innovator can have one or more connections to the tag (print 2).
    The post innovator type was created as a data sheet.
    Now I’m trying to create a google map (print 3) with all the “innovators” post, and I would like to filter the map to tags.
    The problem is that, as we see from the print 3, I do not need all tags but only those associated with post innovators, and being post a registry card, it makes no sense to tag a person (because in the archives tags also appear post type “innovators”), so I decided to simply associate a taxonomy with taxonomies field ACF. In this way it creates a relationship between the post and the tag, but the post is not actually tagged.
    As a result it could use, instead of a list of tags, currently obtained with get_terms, only list the total tags associated with the tassomomie field (no repeats of course, and with the number of associations).
    The final idea is a bit like in print 4.
    Thank you and I hope I was a little bit more clear 🙂

  • Hi @maxdel85

    I’m still not sure about your goal, so please forgive me if I’m wrong.

    I’m afraid you need to use wpdb to achieve it. You can query it to get all of the tags set to the CPT like this:

    SELECT * FROM wp_postmeta WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type = 'innovators') AND meta_key = 'taxonomy_field_name'

    After that, you need to unserialize each row and combine them in one array. Then you can use array_count_values() to count the tags.

    If you want to show the tags based on the custom posts, you need to loop the returned custom posts, get the taxonomy fields and put the IDs in one array. After that, you can use array_count_values() to count it.

    I hope this makes sense.

  • Thank you so much James,
    following your instructions I wrote this code that the public can be useful to others, which makes me a list of the tags used with the number in parentheses. I will use it as a filter for google map. 🙂

    Ps I added “AND meta_value != ‘ ‘” to the query to avoid empty values

    	global $wpdb;
    	$query_tag_post = $wpdb->get_results( 
    	"
    	SELECT * FROM kgt_postmeta WHERE post_id IN (SELECT ID FROM kgt_posts WHERE post_type = 'innovatori') AND meta_key = 'tag_collegato' AND meta_value != ' '
    	"
    );
    
    	
    
    $meta_value = array();
    foreach($query_tag_post as $row) {
       
    	$meta_value[] = unserialize($row->meta_value);
    	
    }
    
    	foreach ($meta_value as $value){
    		foreach ($value as $num_tag){
    			$numeri_tags[] = $num_tag;
    		}
    	}
    	
    	
    
    	foreach ($numeri_tags as $numero_tag){
    		$termine = get_term_by('term_taxonomy_id', $numero_tag, 'settore');
    		$nomi_tag[] = $termine->name;
    		$slug_tag[] = $termine->slug;
    	}
    
    	
    	
    	
    	$stringa = array(); 
    foreach ($nomi_tag as $nome_tag) { 
            $c = array_count_values($nomi_tag); 
            $n = $c[$nome_tag]; 
            $stringa[array_search($nome_tag, $nomi_tag)] = $nome_tag . "(" . $n . ")";    
    } 
    $stringa = implode(',', $stringa); 
    
    echo $stringa; 
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Recall the tags that have a connection to the posts with field taxonomy’ is closed to new replies.