首先,你要跑register_post_type
在…上init
和register_taxonomy
在…上after_setup_theme
之后调用init
. 这意味着您的自定义分类法在注册帖子类型时将不可用。我建议您删除taxonomies
来自的关键字register_post_type
参数数组,然后手动注册分类法。在示例代码中,您似乎创建了两次post类型分类链接。
我也不确定\'query_var\' => true,
在register_taxonomy
参数数组。文档中说您可以将其设置为false
, 或字符串,但不说明如果将其设置为true将发生什么。希望WordPress足够聪明,可以用更有用的东西来代替它,但既然你没有明确地将它设置为除分类名称以外的其他名称,那么现在就删除它(这意味着talktype
将改用)。
我只是把它放在一个空主题中,它似乎工作得很好(即,它打印一个包含元查询的SQL查询)。实际上,运行查询也很好:
functions.php
// Add post types of "Talk" and "Event"
function nc_custom_post_types() {
register_post_type( \'talk\',
array(
\'labels\' => array(
\'name\' => __( \'Talks\' ),
\'singular_name\' => __( \'Talk\' )
),
\'public\' => true,
\'has_archive\' => true,
)
);
// Add new "talktype" taxonomy to "talk" post type
register_taxonomy(\'talktype\', \'talk\', array(
\'hierarchical\' => true,
// This array of options controls the labels displayed in the WordPress Admin UI
\'labels\' => array(
\'name\' => _x( \'Talk Types\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Talk Type\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Talk Types\' ),
\'all_items\' => __( \'All Talk Types\' ),
\'parent_item\' => __( \'Parent Talk Type\' ),
\'parent_item_colon\' => __( \'Parent Talk Type:\' ),
\'edit_item\' => __( \'Edit Talk Type\' ),
\'update_item\' => __( \'Update Talk Type\' ),
\'add_new_item\' => __( \'Add New Talk Type\' ),
\'new_item_name\' => __( \'New Talk Type Name\' ),
\'menu_name\' => __( \'Talk Types\' ),
),
// Control the slugs used for this taxonomy
\'rewrite\' => array(
\'slug\' => \'talktype\',
\'with_front\' => false, // Don\'t display the category base before "/locations/"
\'hierarchical\' => true // This will allow URL\'s like "/locations/boston/cambridge/"
),
));
}
add_action( \'init\', \'nc_custom_post_types\' );
/* For testing purposes
add_action(\'wp\', \'test\');
function test() {
$nextSundayTalkArgs = array(
\'post_type\' => \'talk\',
\'posts_per_page\' => 1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'talktype\',
\'field\' => \'slug\',
\'terms\' => \'sunday-talk\'
)
)
);
$nextSundayTalkQuery = new WP_Query( $nextSundayTalkArgs );
var_dump($nextSundayTalkQuery->request);
die();
}
*/
function sunday_query_args() {
$nextSundayTalkArgs = array(
\'post_type\' => \'talk\',
\'posts_per_page\' => 1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'talktype\',
\'field\' => \'slug\',
\'terms\' => \'sunday-talk\'
)
)
);
return $nextSundayTalkArgs;
}
index.php
<?php get_header(); ?>
<?php query_posts(sunday_query_args()); ?>
<div id="content">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
</article>
<?php endwhile; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
EDIT: 只是试着不加修改地运行代码,这实际上也很有效。据我所知
0 = 1
当未找到指定的分类法或术语时,将生成SQL中的位,这意味着
INNER JOIN
无法创建。确保数据库中有术语,并且术语和分类都显示在帖子类型的编辑屏幕中。
我知道您已经检查并再次检查了数据库中的术语内容,因此,如果这仍然不能解决您的问题,请尝试进一步隔离问题。从使用干净的WordPress安装开始,只添加上面提供的代码,添加talk
发布并将其分配给sunday-talk
学期当我尝试时,效果很好。另外,请尝试直接在数据库上手动运行SQL,如果这样做不起作用,可以放心地说,后/术语关系不存在。生成的SQL查询应该如下所示(请确保更改wp_term_relationships.term_taxonomy_id
尽管如此):
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (4) ) AND wp_posts.post_type = \'talk\' AND (wp_posts.post_status = \'publish\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 1