0

Created custom post type and now I'm trying to create a custom taxonomy. Once I create it and try to access it with get_terms() I get an error of non-object. Var_dump is giving me an empty array object(WP_Error)[1279] public 'errors' => array (size=1) 'invalid_taxonomy' => array (size=1) 0 => string 'Invalid taxonomy.' (length=17) public 'error_data' => array (size=0) empty

Here is the code:

// Custom Post Type for Use Cases page slider

function create_post_type() {
  register_post_type( 'use_cases',
    array(
      'labels' => array(
        'name' => __( 'Use Cases' ),
        'singular_name' => __( 'Use_Case' )
      ),
      'public' => true,
             'supports' => array('title', 'editor', 'custom-fields', 'thumbnail'),
             'hierarchical' => true,
      'has_archive' => true
    )
  );
}
add_action( 'init', 'create_post_type' );
// Custom taxonomy for CPT
function custom_taxonomies() {
    $labels = array(
        'name' => 'Types',
        'singular_name' => 'Type',
        'search_items' => 'Search Types',
        'all_items' => 'All Types',
        'parent_item' => 'Parent Type',
        'parent_item_colon' => 'Parent Type:',
        'edit_item' => 'Edit Type',
        'update_item' => 'Update Type',
        'add_new_item' => 'Add New Custom Type',
        'new_item_name' => 'New Type Name',
        'menu_name' => 'Type'
    );
    $args = array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array('slug'=>'type')
    );

    register_taxonomy('type', array('use_cases'), $args);
}

add_action('init', 'custom_taxonomies');


// Shortcode for Use Cases caption

add_shortcode('Use_Case', 'use_case_shortcode_query');
function use_case_shortcode_query($atts, $content){
  extract(shortcode_atts(array( // a few default values
   'posts_per_page' => '11',
   'post_type' => 'use_cases',
        )
   , $atts));

  global $post;

  $posts = new WP_Query(array( // a few default values
   'posts_per_page' => '11',
   'post_type' => 'use_cases'
        ));
    //print_r($posts);
  $output = '';
$out = '';
    if ($posts->have_posts()):
        while ($posts->have_posts()):
                    //echo var_dump($posts->the_post());
            $posts->the_post();
            /*$out .= '<div class="use_case_boxes">
                <h4>Name: <a href="'.get_permalink().'" title="' . get_the_title() . '">'.get_the_title() .'</a></h4>
                <p class="Film_desc">'.get_the_content().'</p>';
                // add here more...*/
                                $out .= '<a href='.get_post_meta($post->ID, "location", true).'><div class="vce-row vce-element--has-background vce-row--col-gap-30 use_case_finance vce-row-equal-height vce-row-columns--top vce-row-content--top" id="el-6cc8dbe6" data-vce-do-apply="all el-6cc8dbe6"><div class="vce-content-background-container"></div><div class="vce-row-content" data-vce-element-content="true"><div class="vce-col vce-col--md-100p vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first" id="el-f4a5f49c" data-vce-do-apply="background el-f4a5f49c"><div class="vce-col-inner" data-vce-element-content="true" data-vce-do-apply="padding margin  border el-f4a5f49c"><div class="vce-row-container"><div class="vce-row vce-row--col-gap-30 vce-row-columns--top vce-row-content--top" id="el-356c02e5" data-vce-do-apply="all el-356c02e5"><div class="vce-row-content" data-vce-element-content="true"><div class="vce-col vce-col--md-100p vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first" id="el-4c08b71e" data-vce-do-apply="background el-4c08b71e"><div class="vce-col-inner" data-vce-element-content="true" data-vce-do-apply="padding margin  border el-4c08b71e"><div class="vce-row-container"><div class="vce-row vce-row--col-gap-30 vce-row-columns--top vce-row-content--top" id="el-86253324" data-vce-do-apply="all el-86253324"><div class="vce-row-content" data-vce-element-content="true"><div class="vce-col vce-col--md-100p vce-col--xs-1 vce-col--xs-last vce-col--xs-first vce-col--sm-last vce-col--sm-first vce-col--md-last vce-col--lg-last vce-col--xl-last vce-col--md-first vce-col--lg-first vce-col--xl-first" id="el-a1c3851f" data-vce-do-apply="background el-a1c3851f"><div class="vce-col-inner" data-vce-element-content="true" data-vce-do-apply="padding margin  border el-a1c3851f"><div class="vce-google-fonts-heading vce-google-fonts-heading--align-center vce-google-fonts-heading--color-b-72-72-72--45--5C00FF--FF7200"><div class="vce-google-fonts-heading-wrapper"><div class="vce-google-fonts-heading--background vce" id="el-5c7b806f" data-vce-do-apply="border background  padding margin el-5c7b806f"><h2 class="vce-google-fonts-heading-inner" style="font-family: " open="" sans";="" font-weight:="" 400;"="">'.get_the_title().'</h2></div></div></div><div class="vce-single-image-container vce-single-image--align-center"><div class="vce vce-single-image-wrapper" id="el-9632dafd" data-vce-do-apply="all el-9632dafd"><figure><div class="vce-single-image-inner"><img class="vce-single-image" src="http://ai.crafts-world.com/wp-content/uploads/2018/02/Second-home-page-design-Recovered_03.png" data-img-src="http://ai.crafts-world.com/wp-content/uploads/2018/02/Second-home-page-design-Recovered_03.png" alt="" title="Second-home-page-design-Recovered_03"></div></figure></div></div><div class="vce-google-fonts-heading vce-google-fonts-heading--align-center vce-google-fonts-heading--color-empty--45--5C00FF--FF7200"><div class="vce-google-fonts-heading-wrapper"><div class="vce-google-fonts-heading--background vce" id="el-8f301bd3" data-vce-do-apply="border background  padding margin el-8f301bd3"><div class="vce-google-fonts-heading-inner" style="font-family: " open="" sans";="" font-weight:="" 300;"="">'.get_the_content().'</div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></a></a>';

            $out .='</div>';
    /* these arguments will be available from inside $content
        get_permalink()
        get_the_content()
        get_the_category_list(', ')
        get_the_title()
        and custom fields
        get_post_meta($post->ID, 'field_name', true);
    */
    endwhile;
  else:
    return; // no posts found
endif;
  wp_reset_query();
  return html_entity_decode($out);
}

add_filter('acf/settings/remove_wp_meta_box', '__return_false');

// Add metabox for Use Cases CPT to display URL of each box.

function add_event_metaboxes() {
    add_meta_box(
        'use_case_metabox',
        'URL for Use Case Box',
        'wpt_events_location',
        'use_cases',
        'side',
        'default'

    );
}

add_action( 'add_meta_boxes', 'add_event_metaboxes' );

/**
 * Output the HTML for the metabox.
 */
function wpt_events_location() {
    global $post;
    // Nonce field to validate form request came from current site
    wp_nonce_field( basename( __FILE__ ), 'event_fields' );
    // Get the location data if it's already been entered
    $location = get_post_meta( $post->ID, 'location', true );
    // Output the field
    echo '<input type="text" name="location" value="' . esc_textarea( $location )  . '" class="widefat">';
}


/**
 * Save the metabox data
 */
function wpt_save_events_meta( $post_id, $post ) {
    // Return if the user doesn't have edit permissions.
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return $post_id;
    }
    // Verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times.
    if ( ! isset( $_POST['location'] ) || ! wp_verify_nonce( $_POST['event_fields'], basename(__FILE__) ) ) {
        return $post_id;
    }
    // Now that we're authenticated, time to save the data.
    // This sanitizes the data from the field and saves it into an array $events_meta.
    $events_meta['location'] = esc_textarea( $_POST['location'] );
    // Cycle through the $events_meta array.
    // Note, in this example we just have one item, but this is helpful if you have multiple.
    foreach ( $events_meta as $key => $value ) :
        // Don't store custom data twice
        if ( 'revision' === $post->post_type ) {
            return;
        }
        if ( get_post_meta( $post_id, $key, false ) ) {
            // If the custom field already has a value, update it.
            update_post_meta( $post_id, $key, $value );
        } else {
            // If the custom field doesn't have a value, add it.
            add_post_meta( $post_id, $key, $value);
        }
        if ( ! $value ) {
            // Delete the meta key if there's no value
            delete_post_meta( $post_id, $key );
        }
    endforeach;
}
add_action( 'save_post', 'wpt_save_events_meta', 1, 2 );




?>
<ul class="nav nav-tabs nav-justified">
    <li class="active">
        <a data-toggle="tab" href="#all">All</a>
    </li>
    <?php foreach($use_case_categories as $use_case_category) { ?>
        <li>
            <a href="#<?php echo $use_case_category->slug ?>" data-toggle="tab"><?php echo $use_case_category->name ?></a>
        </li>
    <?php } ?>
</ul>

<div class="tab-pane active" id="all">
    <?php
    $args = array(
        'post_type' => 'use_cases',
        'posts_per_page' -1,
        'orderby' => 'title',
        'order' => 'ASC'
    );
    $all_films = new WP_Query( $args );
    ?>

    <?php if ( $all_films->have_posts() ) : // make sure we have films to show before doing anything?>
    <div class="table-responsive">
        <table class="table">
            <?php while ( $all_films->have_posts() ) : $all_films->the_post(); ?>
            <tr>
                <td><?php the_post_thumbnail() ?></td>
                <td><h3><?php the_title() ?></h3</td>
                <td>
                    <p class="lead"><?php the_excerpt() ?></p>
                    <a href="<?php the_permalink() ?>" class="btn btn-primary">Read more</a>
                </td>
            </tr>
            <?php endwhile; ?>
            <?php wp_reset_query() ?>
        </table>
    </div>
    <?php endif; ?>

</div><!-- all films tab pane -->


<?php foreach($use_case_categories as $use_case_category) { ?>
    <div class="tab-pane" id="<?php echo $use_case_category->slug ?>">
        <?php
        $args = array(
            'post_type' => 'use_cases',
            'posts_per_page' -1,
            'orderby' => 'title',
            'order' => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => $use_case_category->slug
                )
            )
        );
        $films = new WP_Query( $args );
        ?>

        <?php if ( $films->have_posts() ) : // make sure we have films to show before doing anything?>
        <table class="table">
            <?php while ( $films->have_posts() ) : $films->the_post(); ?>
            <tr>
                <td><?php the_post_thumbnail() ?></td>
                <td><h3><?php the_title() ?></h3</td>
                <td>
                    <p class="lead"><?php the_excerpt() ?></p>
                    <a href="<?php the_permalink() ?>" class="btn btn-primary">Read more</a>
                </td>
            </tr>
            <?php endwhile; ?>
            <?php wp_reset_query() ?>
        </table>
        <?php endif; ?>

    </div>
1

The error message is pretty clear: Invalid taxonomy..

You're hooking to init to register your custom taxonomy, however you're calling get_terms( 'type' ) right away, before init is fired and therefore before your taxonomy is registered.

You can't call get_terms( 'type' ) before init.

  • What is another good hook other than init then? – Limpuls Mar 13 '18 at 8:17
  • init is just fine and the recommended hook. Just don't do anything before init. – swissspidy Mar 13 '18 at 8:17
  • I moved my taxonomy code with the hook up and the get_terms() is now called after taxonomy register. However, I still get the same problem. I updated my post with full code to be more clear. – Limpuls Mar 13 '18 at 8:20
  • get_terms() isn't being used at all in your post anymore. – Jacob Peattie Mar 13 '18 at 8:31

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.