2

I created a custom post type called 'map' and registered a new taxonomy for it. Now, I need to get, through a loop, every term of 'map'. So far, I had a "invalid taxonomy" which I do not understand why so. Everything is working perfectly except this!

How I register my new taxonomy (in functions.php):

function create_map_taxonomy() {
        $labels = array(
            'name'                       => _x( 'Map_Category', 'taxonomy general name', 'textdomain' ),
            'singular_name'              => _x( 'Map_Category', 'taxonomy singular name', 'textdomain' ),
            'search_items'               => __( 'Search Map_Category', 'textdomain' ),
            'popular_items'              => __( 'Popular Map_Category', 'textdomain' ),
            'all_items'                  => __( 'All Map_Category', 'textdomain' ),
            'parent_item'                => null,
            'parent_item_colon'          => null,
            'edit_item'                  => __( 'Edit Map_Category', 'textdomain' ),
            'update_item'                => __( 'Update Map_Category', 'textdomain' ),
            'add_new_item'               => __( 'Add New Map_Category', 'textdomain' ),
            'new_item_name'              => __( 'New Map_Category Name', 'textdomain' ),
            'separate_items_with_commas' => __( 'Separate Map_Category with commas', 'textdomain' ),
            'add_or_remove_items'        => __( 'Add or remove Map_Category', 'textdomain' ),
            'choose_from_most_used'      => __( 'Choose from the most used Map_Category', 'textdomain' ),
            'not_found'                  => __( 'No Map_Category found.', 'textdomain' ),
            'menu_name'                  => __( 'Map_Category', 'textdomain' ),
        );

        $args = array(
            'hierarchical'          => true,
            'labels'                => $labels,
        );
        register_taxonomy( 'map_category', 'map', $args );
    }
    add_action( 'init', 'create_map_taxonomy', 0 );

And this is my code in footer.php:

<?php 
    $args = array(
        'post_type' => 'map',
    );

    $map_query = new WP_Query($args);

    while ($map_query->have_posts()) : $map_query->the_post();

        $img_background = get_field('image_background');
        $cat = get_terms(get_the_id(), 'map_category');
        var_dump($cat);

    endwhile; 
    wp_reset_query();
    ?>

The result is always "invalid taxonomy". But how is it a invalid taxonomy when I can see it in admin pages?

  • You are trying to get terms earlier than taxonomy is registered. – Max Yudin Jan 16 '18 at 20:45
  • How's that even possible? My code to retrieve the terms are in footer.php. How could I fix this? – Citizen602214085 Jan 16 '18 at 20:46
  • Try to remove priority from add_action() or at least make it >= 1. I don't think zero is allowed. I am not sure, I don't know what happens in the footer except what I see here. – Max Yudin Jan 16 '18 at 20:55
  • I changed the priority from add_action() but I still have the same errors : invalid taxonomy. – Citizen602214085 Jan 16 '18 at 21:36
4

get_terms is not the function you need. This function retrieves an array of WP_Term objects for all terms within the given taxonomy

Update your code to use wp_get_post_terms instead of get_terms. You can keep the same parameters. This function will retrieve the terms used on the current post.

  • EXACTLY! I was reading everything about hooks and filters because I thought it was an issue with the priorities. But I was wrong! A thousand thank you for you sir, you saved my day! – Citizen602214085 Jan 16 '18 at 22:20

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.