0

I created taxonomies for a post type and they work. The problem is that when I add a new post (in post type) in the front end the taxonomy terms I entered in the previous post are displayed by default. In practice, the taxonomy terms for new posts are not reset. Is there a function for this? how can i solve?

add post type

    if ( ! function_exists('bookmaker_post_type') ) {

// Register Custom Post Type
function bookmaker_post_type() {

    $labels = array(
        'name'                  => _x( 'Bookmakers', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Bookmaker', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Bookmakers', 'text_domain' ),
        'name_admin_bar'        => __( 'Bookmakers Reviews', 'text_domain' ),
        'archives'              => __( 'Item Archives', 'text_domain' ),
        'attributes'            => __( 'Item Attributes', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
        'all_items'             => __( 'All Bookmakers', 'text_domain' ),
        'add_new_item'          => __( 'Add New Bookmaker', 'text_domain' ),
        'add_new'               => __( 'Add New Bookmaker', 'text_domain' ),
        'new_item'              => __( 'New Bookmaker', 'text_domain' ),
        'edit_item'             => __( 'Edit Bookmaker', 'text_domain' ),
        'update_item'           => __( 'Update Item', 'text_domain' ),
        'view_item'             => __( 'View Item', 'text_domain' ),
        'view_items'            => __( 'View Items', 'text_domain' ),
        'search_items'          => __( 'Search Item', 'text_domain' ),
        'not_found'             => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
        'featured_image'        => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
        'items_list'            => __( 'Items list', 'text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'Bookmaker', 'text_domain' ),
        'description'           => __( 'Bookmaker review', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'thumbnail', 'revisions', 'custom-fields' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'bookmaker', $args );

}
add_action( 'init', 'bookmaker_post_type', 0 );

}    

Add taxonomy

function bookmaker_taxonomies() {
    
    // Add new taxonomy Languages, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Languages', 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( 'Language', 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( 'Search Languages', 'textdomain' ),
        'all_items'         => __( 'All Languages', 'textdomain' ),
        'parent_item'       => __( 'Parent Language', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Language:', 'textdomain' ),
        'edit_item'         => __( 'Edit Language', 'textdomain' ),
        'update_item'       => __( 'Update Language', 'textdomain' ),
        'add_new_item'      => __( 'Add New Language', 'textdomain' ),
        'new_item_name'     => __( 'New Language Name', 'textdomain' ),
        'menu_name'         => __( 'Language', 'textdomain' ),
    );
 
    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'language' ),
    );
 
    register_taxonomy( 'language', array( 'bookmaker' ), $args );
 
    unset( $args );
    unset( $labels );

add_action( 'init', 'bookmaker_taxonomies', 0 );

Function to display taxonomy terms in front end

 function get_term_list($taxonomy){
    
       $terms = get_terms($taxonomy);
     
       echo '';
     
          foreach ( $terms as $term ) {
     
           // The $term is an object, so we don't need to specify the $taxonomy.
           $term_link = get_term_link( $term );
        
            // If there was an error, continue to the next term.
           if ( is_wp_error( $term_link ) ) {
               continue;
           }
     
           // We successfully got a link. Print it out.
           echo '<a href="' . esc_url( $term_link ) . '">' . $term->name . ", ",'</a>';
    } 
    
   echo '';

   }

Retrieves taxonomy terms and display in front end

<div><?php get_term_list('language'); ?></div>
5
  • I'm not sure I understand your problem, but I do know that calling unset is not a good idea, and highly unusual. Terms don't "reset" as such, can you describe your problem using different words, with examples? – Tom J Nowell Feb 2 at 16:26
  • I have posts for example post A post B post C. Post B and C automatically print the taxonomy terms of post A in the front end. – pasquale Feb 3 at 12:12
  • But they show correctly in the backend? Please update your question to include the code that displays those taxonomies on the frontend, this problem is unrelate to the register_taxonomy call ( also, stop calling unset on $args and $labels ). I assume you are using the standard post editor in WP Admin to create these posts? – Tom J Nowell Feb 3 at 13:36
  • yes in backend it's correct – pasquale Feb 3 at 15:14
  • Ah I understand now, it isn't showing the terms of post A on post B, it's just listing all the terms no matter which post they appear on – Tom J Nowell Feb 3 at 15:37
0

Your code is not listing the terms from post A, it is listing all the terms on your site. You can test this by creating a post Z and adding new terms to Z, you will see those new terms listed on all posts, even post A.

The reason is this:

$terms = get_terms($taxonomy);

get_terms does not get the terms on the current post, it just gets terms.

If you only want the terms the assigned to the current post, you need to use wp_get_post_terms instead

$terms = wp_get_post_terms( get_the_ID(), $taxonomy );

https://developer.wordpress.org/reference/functions/wp_get_post_terms/

2
  • Yes now it works. I was wrong in this. Thank you! – pasquale Feb 3 at 15:56
  • @pasquale If this solves your problem, can you mark it as the accepted answer? It's the tick underneath the voting buttons – Tom J Nowell Feb 3 at 17:53

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.