0

So I think I might be going mad or something. I have a custom post type (arkiv) with two custom taxonomies (genre and author) and I try to list the posts on a template page within a loop and display the post title and the two taxonomies, but I can't get it working. The post title is listed but not the taxonomies.

My CPT looks like this:

function post_archive() {
  $labels = array(
    'name'                => _x( 'Arkiv & Samlingar', 'twentytwenty' ),
    'singular_name'       => _x( 'Arkiv & Samlingar', 'twentytwenty' ),
    'menu_name'           => __( 'Arkiv & Samlingar', 'twentytwenty' ),
    'all_items'           => __( 'Alla Arkiv & Samlingar', 'twentytwenty' ),
    'view_item'           => __( 'Visa Arkiv & Samlingar', 'twentytwenty' ),
    'add_new_item'        => __( 'Lägg till Arkiv & Samlingar', 'twentytwenty' ),
    'add_new'             => __( 'Lägg till Arkiv & Samlingar', 'twentytwenty' ),
    'edit_item'           => __( 'Ändra Arkiv & Samlingar', 'twentytwenty' ),
    'update_item'         => __( 'Uppdatera Arkiv & Samlingar', 'twentytwenty' ),
    'search_items'        => __( 'Sök Arkiv & Samlingar', 'twentytwenty' ),
    'not_found'           => __( 'Fann inget', 'twentytwenty' ),
    'not_found_in_trash'  => __( 'Fann inget i Papperskorgen', 'twentytwenty' ),
  ); 
  $args = array(
    'label'               => __( 'arkiv', 'twentytwenty' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_icon'           => 'dashicons-book',
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => false,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'post',
    'show_in_rest'        => true,
    'taxonomies'          => array('genre', 'author'),
  );   
  register_post_type( 'arkiv', $args );
}
add_action( 'init', 'post_archive', 0 );

And my two taxonomies looks like this:

add_action( 'init', 'genre', 0 );
function genre() {
  $labels = array(
    'name' => _x( 'Subjects', 'taxonomy general name' ),
    'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Subjects' ),
    'all_items' => __( 'All Subjects' ),
    'parent_item' => __( 'Parent Subject' ),
    'parent_item_colon' => __( 'Parent Subject:' ),
    'edit_item' => __( 'Edit Subject' ), 
    'update_item' => __( 'Update Subject' ),
    'add_new_item' => __( 'Add New Subject' ),
    'new_item_name' => __( 'New Subject Name' ),
    'menu_name' => __( 'Subjects' ),
  );    
  register_taxonomy('genre',array('arkiv'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'genre' ),
  ));
}
add_action( 'init', 'author', 0 );
function author() {
  $labels = array(
    'name' => _x( 'Subjects', 'taxonomy general name' ),
    'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Subjects' ),
    'all_items' => __( 'All Subjects' ),
    'parent_item' => __( 'Parent Subject' ),
    'parent_item_colon' => __( 'Parent Subject:' ),
    'edit_item' => __( 'Edit Subject' ), 
    'update_item' => __( 'Update Subject' ),
    'add_new_item' => __( 'Add New Subject' ),
    'new_item_name' => __( 'New Subject Name' ),
    'menu_name' => __( 'Subjects' ),
  );    
  register_taxonomy('author',array('arkiv'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'author' ),
  ));
}

My CPT shows up and the taxonomies too, they are there and I assign taxonomies to the posts in there but then when trying to list the posts I only get the post titles but no taxonomies whatsoever

My query:

<?php $args = array( 
  'post_type' => 'arkiv', 
  'post_status' => 'publish',
  'posts_per_page' => -1, 
  'orderby' => 'date', 
  'order' => 'DESC'
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    <h2><?php the_title(); ?></h2>
    <?php the_terms( $post->ID, 'author', 'Author: ', ', ', ' ' ); ?>
    <?php the_terms( $post->ID, 'genre', 'Genre: ', ', ', ' ' ); ?>
    
  <?php endwhile;
  wp_reset_postdata(); ?>
<?php endif; ?>

Can't understand what I'm doing wrong, thanks in advance.

1 Answer 1

0

You haven't exposed the global $post variable after calling $the_query->the_post().

Either add global $post right after calling $the_query->the_post(), or replace $post->ID with get_the_ID().

1
  • Thank you very much! That solved it :)
    – Triphys
    Commented Nov 16, 2021 at 14:26

Your Answer

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

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