0

i have a stupid situation, where i somehow can't find a solution and would really need some help!

So... i'm working on a project, where i have in addition to Post and Pages two Custom post types: Photos and Events.

I have created a custom taxonomy named "Tags". This taxonomy is applied to Post, Pages, Photos and Events... so basically you can give everything a Tag and you can chose everywhere from tags, that are being used elsewhere (example: if you give some tags in the Events, then you can later chose the same Tags in Photos).

The Idear is to show everywhere related stuff. So if you are on a Event, you will see in the sidebar related news, photos, even page entries...and so on...

NOW i want to display in the Photo - section only the tags, that were already used in the Custom post type Photos. Not all of them (there are some tags in Events, that have never been used in photos). So one could chose to see only photos base on a Tag...

I don't have any code yet... although i'm not a newbi in wordpress, a this point i don't even know where to start.

how can one display a list of tags, that are a custom taxonomy, limited by a custom post type?!

Any suggestion would be really nice!

  • Why are you creating a taxonomy called "tags" when wp already has "tags" functions by default. – Wyck Mar 31 '11 at 17:23
  • tnx for your respond, i know that wordpress has tags by default, but the default tags don't show up in custom post types. or somehow i wasn't able to do that... so i've just create a custom taxonomy and assign it to everything i want. note, it's very important for the project, that the tags are shared between post types – CRIS Apr 1 '11 at 2:34
  • Tags do work for custom posts types, you should not name a custom taxonomy after something already used by WordPress, use a unique name or you will probably run into problems. – Wyck Apr 1 '11 at 5:11
1

To use the built-in tag functionality with a custom post type, you need to add this argument when registering your post type:

'taxonomies' => array( 'post_tag' ),

So to register a post type events, you'd do (eg):

register_post_type( 'events', array(
    'show_ui' => true,
    'taxonomies' => array( 'post_tag' )
   // etc ...
) );
  • aaa, tnx for that Daniel! – CRIS Jun 8 '11 at 5:49
1

So, i have figured out my problem, and come up with this function

function getCustomTaxonomy($posttype,$taxonomy) { $custompostsid = get_posts('post_type='.$posttype);

$i=0;
$j=0;

foreach ($custompostsid as $post)
{  
    setup_postdata($post);

    $customtags = wp_get_post_terms( $post->ID , $taxonomy);

        foreach ($customtags as $tag) 
        {
            $tagid[$j]=$tag->term_id; 
            $j++;
        }

    $i++;
}

$alltagsid=array_unique($tagid);

$blogurl=get_bloginfo('url');

$nummberofelements=count($alltagsid);

$k=0;

foreach ($alltagsid as $onetagid)
{

 $array = get_term_by('id', $onetagid, $taxonomy, 'ARRAY_A'); 
 $tagname=print_r($array[name],true);
 $tagcount=print_r($array[count],true);
 $taglink=$blogurl."/?".$taxonomy."=".$tagname."&type=".$posttype;

 echo "<a class='taxonomy-link' href='".$taglink."'>";
     echo $tagname." (".$tagcount.")";
 echo "</a>";
 $k++;

 if ($k<$nummberofelements) echo ", ";
}

}

i works, but i know it's not the best way to do it... cos if you have a lot of post and tags, it could get havy... so i'm still searching, but until i find a better way, this should do it :)

feel free, to optimize the code ;)

best regards

0

Have you seen this: http://wordpress.org/support/topic/passing-current-single_tag_title-as-variable-into-new-wp_query

it may help to get you started perhaps?

  • tnx, for your respond, but unfortunately this dosen't help me alot... but don't worry, i have already started, and have manage to somehow get the IDs from tags that are used in the custom post type... i was also able to print of the name of the tag based on the id, but somehow i have problem with the links...get_tag_link($tagid) dosen't return anything in my case... – CRIS Apr 1 '11 at 2:41

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.