3

I use More Plugin (More Types, More Fields and More Taxonomy) to create custom taxonomies, post types and fields.

I've created a select control filled with taxonomies (looping through $wp_taxonomies).

What I need is to show a list of post types associated with currently selected taxonomies (I will do this by using jQuery) and I didn't figure out how to get such a list of post types. Thanks!

1
  • 1
    $wp_taxonomies['YOURTAXNAME']->object_type holds an array of post types that use the given taxonomy.
    – t31os
    Feb 10, 2011 at 13:15

2 Answers 2

1

You retrieve it with $wp_taxonomies already. I just wrote it down, so you'll probably have to try a little to get the following lines of code up and running, but it should give you an idea of how it should work (see it as reference/pseudo code):

EDIT: After a note from Mike Schinkel i updated the code to make it easier to ignore built in taxonomies (see: _builtin). I hope Mike will post the example he sent me here, so his (much easier) sollution can be marked as the final approach...

// equals the following $keys
$wp_taxonomies['category'] == $wp_taxonomies[0];
$wp_taxonomies['post_tag'] == $wp_taxonomies[1];
$wp_taxonomies['nav_menu'] == $wp_taxonomies[2];
$wp_taxonomies['link_category'] == $wp_taxonomies[3];

// after $key #3 you retrieve all different registered taxonomies
$all_tax = count($wp_taxonomies)-4; // gives you the amount of reg. tax.

// Here starts the actual code
$post_types = array(); // some empty array to save your post types for further procesing
$i = 0;
foreach ( $wp_taxonomies as $tax ) {
if ( !$tax->_builtin)
  $post_type_arr = $tax[$i]->object_type; // array of post types in the current taxonomy
  foreach ( $post_type_arr as $arr ) : // loop through all post types assigned to the taxonomy
    $post_types[] .= $arr; // assign them to our array of post types
  endforeach;
  $i++;
}
$post_types = array_unique($post_types); // drop doublettes out of the array
var_dump($post_types);

# expected result close to this...

# Array(
#   1 => $post_type_a,
#   2 => $post_type_b,
#   3 => $post_type_n
# );
4
  • 1
    Slight syntax error in your code mate, enforeach; should be endforeach;.. ;)
    – t31os
    Feb 10, 2011 at 13:07
  • Thanks kaiser! With some minor changes your code was I looking for... Feb 10, 2011 at 13:43
  • @Valentin & t31os: n/p. Could you please edit my code, so other people may reuse it? @Valentin: Please mark it as the sollution (Check on left side below voting). Thanks.
    – kaiser
    Feb 10, 2011 at 14:32
  • See my edit above: Please do not mark it as the sollution.
    – kaiser
    Feb 10, 2011 at 17:48
1

So this is the real solution:

function get_object_types_unique( $all_r_custom = false ) {
    $tax_object = get_taxonomies( array( '_builtin' => $all_r_custom ), 'objects' );
    if ( $all_r_custom == true )
        $tax_object = array_merge( $tax_object, get_taxonomies( array( '_builtin' => false ), 'objects' ) );

    $object_types = array();
    foreach ( $tax_object as $tax ) {
        $object_types = array_merge( $object_types, $tax->object_type );
    }

    $object_types = array_unique( $object_types );
    return $object_types;
}

It allows to either retrieve only custom post/object types with get_object_types_unique() or all incl. built in object types get_object_types_unique( true ). So far there's with this function no chance to retrieve only the built in object types, because i can't see a need so far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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