我正在创建一个音乐数据库网站,其中包含两种自定义帖子类型:曲目和专辑。我已经将这些曲目链接到各自的专辑,并为专辑的slug定制了分类法。在每个相册页面上,我都试图通过检索所有曲目来获取相册的曲目列表dub_album_slug
与当前相册的slug匹配的分类法。以下是我的查询代码:
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 );
目前,它根本没有输出任何东西。即使我手动输入一个我知道存在的术语,并且有与之相关的帖子,也不会发生任何事情。
下面是我用来输出数据的代码:
<?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.
这是我的自定义帖子类型注册:
//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 );
}
这是我的自定义分类注册:
//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 );
}
这是我从中得到的
$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
我对MySQL或WordPress数据库中的分类法不太熟悉。
我只是想到了一些可能相关的事情。我正在使用我找到的一些代码按曲目编号分类法对曲目进行排序。它直接修改MySQL查询,所以我想这可能会导致问题。这是:
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 );
我希望你能帮助我。如果您需要更多代码,请告诉我。干杯