0

I'm creating a music database site with two custom post types: tracks and albums. I've linked the tracks to their respective albums with a custom taxonomy for the album's slug. On each album page, I'm trying to get a tracklist of the album by retrieving all tracks with a dub_album_slug taxonomy that matches the slug of the current album. Here's my code for that query:

global $post;
$post_slug = $post->post_name;
$args = array (
    'post_type' => 'dub_track',
    'tax_query' => array(
        array(
            'taxonomy' => 'dub_album_slug',
            'field'    => 'name',
            'terms'    => $post_slug,
        ),
        'orderby' => 'dub_track_no',
        'order' => 'ASC',
    ),
);
$tracks_query = new WP_Query( $args );

At the moment, it doesn't output anything at all. Even if I manually put in a term that I know exists and has posts associated with it, nothing happens.

Here's the code I use to output the data:

<?php if ($tracks_query->have_posts()) : ?>
    <?php while ($tracks_query->have_posts()) : $tracks_query->the_post(); ?>
        <?php echo get_the_term_list( $post->ID, 'dub_original_artist', '', ', ' ); ?>
etc.

Here's my custom post type registration:

//Register Track Custom Post Type
if ( ! function_exists('dub_track_custom_post_type') ) {

    // Register Custom Post Type
    function dub_track_custom_post_type() {

        $labels = array(
            'name'                => _x( 'Tracks', 'Post Type General Name', 'text_domain' ),
            'singular_name'       => _x( 'Track', 'Post Type Singular Name', 'text_domain' ),
            //...
        );
        $rewrite = array(
            'slug'                => 'track',
            'with_front'          => true,
            'pages'               => true,
            'feeds'               => false,
        );
        $args = array(
            'label'               => __( 'dub_track', 'text_domain' ),
            'description'         => __( 'Dub Tracks', 'text_domain' ),
            'labels'              => $labels,
            'supports'            => array( 'title', 'editor', 'revisions', 'custom-fields', ),
            'taxonomies'          => array( 'track-no', 'original-artist', 'original-title', 'original-released', 'producer', 'comment', 'album-slug', 'artist', 'riddim' ),
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 5,
            'can_export'          => true,
            'has_archive'         => true,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'rewrite'             => $rewrite,
            'capability_type'     => 'post',
        );
        register_post_type( 'dub_track', $args );

    }

    // Hook into the 'init' action
    add_action( 'init', 'dub_track_custom_post_type', 0 );

}

Here's my custom taxonomy registration:

//Register Album Slug Custom Taxonomy
if ( ! function_exists( 'dub_album_slug' ) ) {

    // Register Custom Taxonomy
    function dub_album_slug() {

        $labels = array(
            'name'                       => _x( 'Album Slug', 'Taxonomy General Name', 'text_domain' ),
            'singular_name'              => _x( 'Album Slug', 'Taxonomy Singular Name', 'text_domain' ),
            //...
        );
        $rewrite = array(
            'slug'                       => 'album-slug',
            'with_front'                 => true,
            'hierarchical'               => false,
        );
        $args = array(
            'labels'                     => $labels,
            'hierarchical'               => false,
            'public'                     => true,
            'show_ui'                    => true,
            'show_admin_column'          => true,
            'show_in_nav_menus'          => true,
            'show_tagcloud'              => true,
            'rewrite'                    => $rewrite,
        );
        register_taxonomy( 'dub_album_slug', array( 'dub_track' ), $args );

    }

    // Hook into the 'init' action
    add_action( 'init', 'dub_album_slug', 0 );

}

Here's what I get from $tracks_query->request:

SELECT SQL_CALC_FOUND_ROWS dub_posts.ID FROM dub_posts INNER JOIN dub_term_relationships ON (dub_posts.ID = dub_term_relationships.object_id)
LEFT OUTER JOIN dub_term_relationships ON dub_posts.ID=dub_term_relationships.object_id
LEFT OUTER JOIN dub_term_taxonomy USING (term_taxonomy_id)
LEFT OUTER JOIN dub_terms USING (term_id) WHERE 1=1 AND ( dub_term_relationships.term_taxonomy_id IN (16) ) AND dub_posts.post_type = 'dub_track' AND (dub_posts.post_status = 'publish') AND (taxonomy = 'dub_track_no' OR taxonomy IS NULL) GROUP BY object_id ORDER BY GROUP_CONCAT(dub_terms.name ORDER BY name ASC) ASC LIMIT 0, 20

I'm not great with MySQL or how taxonomies work in the WordPress database.

I just thought of something that may be relevant. I'm using some code I found to order the tracks by the track number taxonomy. It modifies the MySQL query directly so I guess it could be causing a problem. Here it is:

function orderby_tax_clauses( $clauses, $wp_query ) {
global $wpdb;
$taxonomies = get_taxonomies();
foreach ($taxonomies as $taxonomy) {
    if ( isset( $wp_query->query['orderby'] ) && $taxonomy == $wp_query->query['orderby'] ) {
        $clauses['join'] .=<<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
SQL;
        $clauses['where'] .= " AND (taxonomy = '{$taxonomy}' OR taxonomy IS NULL)";
        $clauses['groupby'] = "object_id";
        $clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";
        $clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
    }
}
return $clauses;

}

add_filter('posts_clauses', 'orderby_tax_clauses', 10, 2 );

I hope you can help me out. Let me know if you need more code. Cheers

20
  • 2
    what happens if you change 'field' => 'name' to 'field' => 'slug'?
    – Milo
    Nov 3 '14 at 16:31
  • That doesn't help I'm afraid. Anyway, in this case, they're usually the same thing. Nov 3 '14 at 16:34
  • Have you checked if the actual taxonomy does exist? codex.wordpress.org/Function_Reference/taxonomy_exists Nov 3 '14 at 16:36
  • var_dump($tracks_query) and check the query vars and generated SQL, should give you an idea of what's wrong.
    – Milo
    Nov 3 '14 at 16:38
  • How do you output the data? (the code givens doesn't display anything, it just stores the WP Query object in $tracks_query-variable. Nov 3 '14 at 16:38
0

@Milo found that the problem was with the code to order by the track number taxonomy. I'm going to make track number a custom field.

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.