0

I have this code:

        $post_types = get_post_types();


    if($post_types)
    {
        foreach($post_types as $post_type)
        {
            $post_type_object = get_post_type_object($post_type);
            $taxonomies = get_object_taxonomies($post_type);
            if($taxonomies)
            {
                foreach($taxonomies as $taxonomy)
                {
                    if(!is_taxonomy_hierarchical($taxonomy)) continue;
                    $terms = get_terms($taxonomy, array('hide_empty' => false));
                    if($terms)
                    {
                        foreach($terms as $term)
                        {
                            $value = $taxonomy . ':' . $term->term_id;

                            if( $simple_value )
                            {
                                $value = $term->term_id;
                            }

                            $choices[$post_type_object->label . ': ' . $taxonomy][$value] = $term->name; 
                        }
                    }
                }
            }
        }

This will list all of the Taxonomy I have. But I only need thoose taxonomies which has product_cat category. Any suggestion how could I change this?

0

Before your first foreach loop, at the top of the script, define an empty array to house what you're after:

$taxWith_product_cat = array();

Now since you're looping through all taxonomies and all terms therein, in the terms loop foreach($terms as $term), check if the current term is product_cat. If it is, add the terms taxonomy to the new array:

if ($term->slug == 'product_cat ')
    $taxWith_product_cat[] = $taxonomy;

There you have it. Now at the end of the script, you have an array of taxonomies that contain a term of product_cat:

echo "<pre>".print_r($taxWith_product_cat,true)."</pre>";

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.