Ask your WordPress questions! Pay money and get answers fast! (more info)

Custom Taxonomies & Post Types - Options WordPress

  • SOLVED

I am trying to build a plugin that dynamically populates custom taxonomies and post types in drop downs with an options page. To date, I am using the code below to dynamically display custom post types. I am following a similar pattern for custom taxonomies.

My problem: The code below only returns builtin post types and taxonomies, it does not display any custom ones. I know that my custom post types and taxonomies are correctly registered as I can display them successfully outside this plugin.

I would prefer to stick with this method as I have built a significant amount of options, but I am stuck on this - custom taxonomies and post types. Thanks!


$wp_custom_taxonomy_args = array('public' => true);
$woo_wp_custom_taxonomies = array();
$woo_wp_custom_taxonomies = get_taxonomies($wp_custom_taxonomy_args,'objects');
$woo_wp_custom_taxonomies_formatted = array();
$woo_wp_custom_taxonomies_formatted['none'] = 'None';
foreach ($woo_wp_custom_taxonomies as $woo_wp_custom_taxonomy) {
$woo_wp_custom_taxonomies_formatted[$woo_wp_custom_taxonomy->name] = $woo_wp_custom_taxonomy->label; }


$qsoptions = array (
array (
'name' => 'Custom Post Types',
'id' => $shortname.'select_post_types',
'type' => 'select',
'options' => $woo_wp_custom_post_types_formatted
),
);

!-- Dropdown select -->
<?php } elseif ($value['type'] == 'select') { ?>

<div class="wrap">
<label for="<?php echo $value['id']; ?>"><h3><?php echo $value['name']; ?></h3></label>
<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
<?php foreach ($value['options'] as $qsoption) { ?>
<option <?php if ( get_option( $value['id'] ) == $qsoption) { echo ' selected="selected"'; } elseif ($qsoption == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $qsoption; ?></option>
<?php } ?>
</select><br />
<div class="note"><?php echo $value['note']; ?></div>

</div><!-- .wrap -->

Answers (2)

2014-05-05

John Cotton answers:


<?php
$args = array(
'public' => true,
'_builtin' => false

);
$taxonomies = get_taxonomies( $args,'objects' );
$post_types = get_post_types( $args,'objects' );

will get you custom taxonomies and post types.


Louise comments:

Hello - I'm not sure that answers my specific question. I need assistance with getting custom post types and taxonomies in my plugin options page. If I were to use the parameter '_builtin' => false as you suggest, it would return no values, which is my problem. Please help by providing more explanation for my specific question. Thanks.


John Cotton comments:

So you've tried the code above and you get nothing?

If so, is is an order thing - ie are you sure your post types and taxonomies are registered at the point the above code runs?

So, for example if the above code ran in the init hook and you didn't registered your post types until the wp_head hook, the code above would return nothing.


Louise comments:

Yes, this is a good point, but I do not know how to fix it. I should mention if I follow the same pattern with get_categories above, my custom created categories do appear. The code above returns the builtin taxonomies and post types, just not my custom ones. My custom taxonomies and post types are registered with the recommended init hook.

I have the code above in a separate php file within the "includes" folder (i.e. not the main php file). The code is run within the custom menu page function, which builds the form/content of my options page. How do I go about ensuring the custom taxonomies & post types are registered at the point the menu page is created? Thanks.


John Cotton comments:

Just to be absolutely clear:

When you run <em>my</em> code with _builtin set to false, do you get anything returned?

<blockquote>How do I go about ensuring the custom taxonomies & post types are registered at the point the menu page is created?</blockquote>

From what you've said, it would be odd if the registration hadn't taken place at that point.

Do you see the options page displayed with your custom post types in the dashboard menu on the left? Can you send a screen grab or link?


Louise comments:

I'm not sure I understand, where should I be running your code? Outside of this plugin, I can successfully call taxonomies using the below from the codex. Is that what you mean?

<?php
$args = array(
'public' => true,
'_builtin' => false

);
$output = 'names'; // or objects
$operator = 'and'; // 'and' or 'or'
$taxonomies = get_taxonomies( $args, $output, $operator );
if ( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
echo '<p>' . $taxonomy . '</p>';
}
}
?>

I attached a screen shot of my plugin menu page. "Outfits" is my Custom Post Type, which appears in the dashboard menu. However, you can see in the drop down (Set to public => true) it doesn't appear. Thanks.