• Resolved webmistress666

    (@webmistress666)


    I’m trying to number my custom taxonomy, like so:

    Custom Post Type = “Books”
    Custom Taxonomy = “Series”

    So that it will print out “Book [number] in Series BlahBlah”, ordered by post date.

    It’s making my brain hurt. I’m sure I probably need some kind of walker or… something? This is the code I’m using to output my taxonomy name and link, and it’s inside the post loop:

    <?php
    $terms = get_the_terms( $post->ID, 'series' );
    if ( $terms && ! is_wp_error( $terms ) ) :
      $series_links = array();
      foreach ( $terms as $term ) {
        $series_links[] = $term->name;
      }
      $seriestitle = '<h3>Book '. $i .' In Series: <a href="'. home_url( '/' ).'series/' . $term->slug . '" title="' . sprintf(__('View all books in the %s series', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a></h3>';
      echo $seriestitle
    endif;
    ?>

    Any advice is super appreciated!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this function in your theme’s functions.php:

    function get_count_in_series( $post, $taxonomy = 'category', $term_id = false ) {
    
      $post_id = absint( $post->ID );
    
      if ( !$post_id && !$term_id)
        return;
    
      global $wpdb;
    
      $query = "
        SELECT COUNT('ID') as count FROM $wpdb->posts p
        INNER JOIN $wpdb->term_relationships tr ON(p.ID = tr.object_id)
        INNER JOIN $wpdb->term_taxonomy  tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id )
        WHERE p.post_type = %s AND p.post_date <= %s  AND p.post_status = 'publish' AND tt.taxonomy = %s AND tt.term_id = %d
        ORDER BY p.post_date DESC";
    
      $query = $wpdb->prepare( $query, $post->post_type, $post->post_date, $taxonomy, $term_id );
      $results = $wpdb->get_results( $query );
    
      if ( isset($results[0]->count ) )
        return $results[0]->count;
    
      return;
    }

    Now you can use the function get_count_in_series() inside the loop:

    <?php
    $taxonomy = 'series';
    // get the post terms
    $terms = get_the_terms( $post->ID, $taxonomy );
    if ( $terms && !is_wp_error( $terms ) ) {
      foreach ( $terms as $term ) {
    
        $number = get_count_in_series( $post, $taxonomy, $term->term_id );
        $url = get_term_link( $term, $taxonomy );
    
        if ( !is_wp_error( $url ) ) {
          echo '<h3>Book '. $number .' In Series: <a href="'. $url . '" title="' . sprintf( __( 'View all books in the %s series', 'my_localization_domain' ), esc_attr( $term->name ) ) . '">' . $term->name . '</a></h3>';
        }
      }
    }
    ?>

    Thread Starter webmistress666

    (@webmistress666)

    Total, absolute, unadulterated perfection.

    Thanks soooo much!

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. I’m glad you’ve got it resolved 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Numbered Output for Custom Taxonomy’ is closed to new replies.